面试时间:2018 年 9 月 7 日,周五,下午两点。
PHP
1. 数组 $arr = [1, 2, 3,…],如何在数组里追加一个元素x,请给出至少两种代码
array_push($arr, x);
$arr[count($arr)] = x;
2. 下面的代码,输出是 true 还是 false?
is_null(0)
is_null([])
empty(false)
empty(0)
false == []
null == []
false false true true true true
3. 现有一个字符串,$str = “欢迎来到享换机,You are welcome!”,请给出代码,截取前 7 个文字
echo mb_substr($str, 0, 7);
echo substr($str, 0, 21);
4. 请写一个正则表达式验证电子邮件的格式是否正确
^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]+\.[A-Za-z]{2,4}$
5. 给出下面代码的结果
$a = 1;
$b = $a;
$c = &$a;
$d = $c;
$e = &$c;
$e = 2;
echo $a, "\n", $b, "\n", $c, "\n", $d, "\n", $e, "\n";
2
1
2
1
2
6. 请简述Session原理,以及使用Session时的注意事项
https://www.jianshu.com/p/2b7c10291aad
7. 已有数组$arr = [1, 2, 3…] 以及变量 $a,使用 array_map 函数,生成新的数组,其每个元素在 $arr 每个元素的基础上加 $a。请给出代码
return array_map(function($v) use ($a) {
return $v + $a;
}, $arr);
8. 请给出代码,输出当月最后一天的当前时间,格式:年-月-日 时:分:秒。eg:
当前时间:2018-05-10 22:05:35
输出时间:2018-05-31 22:05:35
echo date('Y-m-d H:i:s', strtotime("last day of this month"));
9. PHP 怎么避免 XSS 攻击?怎么避免 SQL 注入?
https://www.cnblogs.com/ITtangtang/p/3982297.html
10. 请给出下面代码在php7下的结果。如有错误,请指出
declare(strict_types=0);
static $c = 1;
$d = 3;
$res1 = test(1.5, 2);
echo $res1, "\n";
$res2 = test(1.5, 2);
echo $res1, "\n";
function test (int $a, int $b) : int {
static $d = 4;
$res = $a + $b;
if (isset($c)) {
$res += $c;
}
if (isset($d)) {
$res += $d;
$d += 1.5;
}
return $res;
}
7
7
数据结构 & 算法
1. n 只猴子,围成 1 圈坐,依次按 1、2、3、…、m循环报数,报到 m 的淘汰。请写出代码,求最后剩下的猴子。
function monkey($n, $m) {
$out = [];
$pointer = 0;
for ($i = 1; $i <= $n - 1; $i++) {
echo "round ", $i, ": ";
$count = 0;
while ($count < $m) {
$pointer = $pointer % $n + 1;
if (!in_array($pointer, $out)) {
$count++;
if ($count == $m) {
echo $pointer, "(out)\n";
} else {
echo $pointer, ", ";
}
}
}
$out[] = $pointer;
}
for ($i = 1; $i <= $n; $i++) {
if (!in_array($i, $out)) {
return $i;
}
}
}
2. 请写出代码,构造单向链表。
class ListNode {
public $val;
public $next;
public function __construct($val) {
$this->val = $val;
$this->next = null;
}
}
$array = [1, 2, 3, 4, 5];
foreach ($array as $key => $val) {
if ($key == 0) {
$head = $node = new ListNode($val);
} else {
$node->next = new ListNode($val);
$node = $node->next;
}
}
return $list;
3. 有两个数组 [1, 4, 7, 8, 9], [2, 3, 7, 8, 10],请写出代码,将两个数组合并成一个升序的数组。
$arr1 = [1, 4, 7, 8, 9];
$arr2 = [2, 3, 7, 8, 10];
$arr = array_merge($arr1, $arr2);
sort($arr);
MySQL
1. 取表 A 里面 status 字段为 1 的 1000 到 1010 行数据,请写出 sql
select * from A where status = 1 limit 10, 1000
2. 请简述 left join, inner join, right join 的区别
https://stackoverflow.com/questions/448023/what-is-the-difference-between-left-right-outer-and-inner-joins
3. 表 A 有一个联合索引 idx(a, b, c)。下面的情况,是否可以使用到索引
select * from A where b = 2;
select * from A where a = 2;
select * from A where a > 2;
select * from A where c = 1;
select * from A where a = 1 and c = 2;
select * from A where a in (1, 2, 3) and c = 2;
No Yes Yes No Yes Yes
逻辑
1. 1 块钱可以买 2 瓶汽水,2 个瓶盖可以换 1 瓶汽水。问十块钱最多可以喝到多少瓶汽水?
40
2. 1000 个瓶子中有一瓶毒药,一只老鼠吃到毒药后一周才会死亡。如果要检测出有毒药的一瓶,问至少需要多少只老鼠?
一周时间:10 只老鼠
两周时间: 7 只老鼠
三周时间: 5 只老鼠
500 total views, 2 views today