likes
comments
collection
share

盘一盘最近遇到的面试题

作者站长头像
站长
· 阅读数 5

前言

今年由于疫情,各大公司得招聘要求及招聘难度越来越大,所以跳槽相对来说也很难。如果面试的结果一般对方公司也会使劲的压职级、压薪资。一句话概括就是:今年真的太难啦~😭,当然对于优秀选手来说不存在什么世道艰难,出去照样随便收割offer。所以希望看到这篇文章的每个看官都能成为这样的强者,吊打面试官。接下来就分享下遇到的一些高频面试题,有阿里、腾讯、字节跳动、美团、快手。总体来说今年各大公司都看重手写代码,尤其算法,几乎每轮至少两道。

一、理论题

ajax和fetch区别

1、从 fetch()返回的 Promise 将不会拒绝HTTP错误状态, 即使响应是一个 HTTP 404 或 500。相反,它会正常解决 (其中ok状态设置为false), 并且仅在网络故障时或任何阻止请求完成时,它才会拒绝。 2、默认情况下, fetch在服务端不会发送或接收任何 cookies, 如果站点依赖于维护一个用户会话,则导致未经认证的请求(要发送 cookies,必须发送凭据头). 链接

tcp为什么4次挥手而不是3次

点击查看😄

h5和端通信

两种方法:一种是通过JSBridge的方式,另一种是通过schema的方式

/**
 * 作用域下的JSBridge,
 * 和实例化后的getNetInfomation,
 * 均根据实际约定情况而定,
 * 这里只是用来举例说明
 */
const bridge = window.JSBridge;
console.log(bridge.getNetInfomation());

点击查看详情😄

react fiber

react fiber看这篇就够了😄

Object.getOwnPropertyNames和Object.keys区别

Object.getOwnPropertyNames
Object.keys 区别
总结:Object.keys主要用于遍历对象自有的可枚举属性,不包括继承自原型的属性和不可枚举的属性。
Object.getOwnPropertyNames主要用于返回对象的自有属性,包括可枚举和不可枚举的属性,不包括继承自原型的属性。
如果传入参数为数组:
Object.keys返回数组对象的index 值
Object.getOwnPropertyNames返回数组对象的index值和length

描述http复杂请求和简单请求、option

描述一下nodejs时间循环,I/O发生在那个阶段

commonjs和esmodule区别、是否可以循环引入

描述react事件合成机制

双击666😄

二、代码输出相关问题

1、let、var区别及块级作用域

if (!('a' in window)) {
    var a = 1;
    // let a = 1;
}
// let和var分别输出什么
console.log(a); // undefined,报错

2、class继承

class A {
    constructor() {
        this.name = 'xiaoming';
    }
    sayname() {
        console.log(this.name);
    }
}
class B extends A {
    constructor() {
        super();
    }
}
let b = new B();
A.prototype.age = 18;
A.prototype.name = '小明';
console.log(b.age, b.name);

3、考察数组的方法是否对原数组有影响

var arr = [1,2,3,4,5]; 
arr.pop(); 
arr.concat([6,7]); 
arr.push(8); 
arr.reverse(); 
console.log(arr);

4、css画一个屏幕宽度一半的正方形

div {
	width: 50vw;
    height: 50vw;
}
div {
	width: 50%;
    padding-top: 100%;
}

三、手写代码题

1、实现prmises.all

function promiseall(list) {
  let res = [];
  return new Promise((resolve, reject) => {
    for (const v of list) {
      v.then((result) => {
        res.push(result);
        if(res.length === list.length){
          resolve(res)
        }
      }, reject);
    }
  })
}

2、判断一个对象是否重复引用

const aa = {
    a: 'b'
    b: aa;
}
obj(obj, 'aa.b') // 返回true/false
function obj(obj, key) {
 	// 大家评论后回复吧....
}

3、随机数排序算法

// 给定一个数组,返回随机排序后的数组
/*
* @file fisher-yates随机算法
* 从最后一位开始将当前位置和一个随机位置交换
* */
function shuffle(arr) {
    for (let i = arr.length - 1; i > 0; i--) {
        let randomIndex = Math.floor(Math.random() * (i + 1));
        [arr[i], arr[randomIndex]] = [arr[randomIndex], arr[i]];
    }
    return arr;
}

4、两棵二叉树,判断a是否是b的子树 5、sku算法

function sku(list) {
    let res = [];
    /*
    * @params chunkIndex要处理的list属性下标,如颜色1
    * @params prev 前一个拼接好的数组,如[iphone xs, 白色]
    * */
    function helper(chunkIndex, prev) {
        let curList = list[chunkIndex];
        let isLatest = chunkIndex === list.length - 1;
        for (let v of curList) {
            let cur = prev.concat(v);
            if (isLatest) {
                res.push(cur);
            } else {
                helper(chunkIndex + 1, cur);
            }
        }
    }
    helper(0, []);
    return res;
}
let skuList = [
    ['iPhone X', 'iPhone XS'],
    ['黑色', '白色'],
    ['64g', '256g']
];
console.log(sku(skuList));

6、扁平化数组

function flat(arr) {
    return arr.reduce((cur, item) =>
    Array.isArray(item) ? flat(item) : cur.concat(item), []);
}

7、实现一个bind函数

Function.prototype.myBind = function(that, ...args) {
  if (typeof this !== 'function') {
    throw new TypeError('Error')
  }
  const self = this;
  // new优先级
  var fbound = function() {
    self.apply(this instanceof self ? this : that, args.concat(Array.prototype.slice.call(arguments)));
  };
  // 继承原型上的属性和方法
  fbound.prototype = Object.create(self.prototype);
  return fbound;
};

8、实现debounce和throttle函数

// 防抖
function debonce(fun, wait) {
    let timer = null;
    return function (...args) {
        if (timer) clearTimeout(timer);
        timer = setTimeout(() => {
            fun.apply(this, args);
        }, wait);
    }
}
// 节流
function throttle(fn, wait) {
    let timer = null;
    return function (...args) {
        if (!timer) {
            timer = setTimeout(() => {
                fn.apply(this, args);
                timer = null;
            }, wait);
        }
    }
}

9、实现new 方法

function mynew(con, ...args) {
    const obj = {};
    Object.setPrototypeOf(obj, con.prototype);
    let result = con.apply(obj, args);
    return result instanceof Object ? result : obj;
}

10、判断两个对象是否相等

// 对Object扩展一个方法chargeObjectEqual
Object.prototype.chargeObjectEqual = function(obj){
    // 当前Object对象
    var propsCurr = Object.getOwnPropertyNames(this);
    // 要比较的另外一个Object对象
    var propsCompare = Object.getOwnPropertyNames(obj);
    if (propsCurr.length != propsCompare.length) {
        return false;
    }
    for (var i = 0,max = propsCurr.length; i < max; i++) {
        var propName = propsCurr[i];
        if (this[propName] !== obj[propName]) {
            return false;
        }
    }
    return true;
}

11、判断一个点在三角形内

function pointInTriangle(x1, y1, x2, y2, x3, y3, x, y) {
    // a = ((y2 - y3) * (x - x3) + (x3 - x2) * (y - y3)) / ((y2 - y3) * (x1 - x3) + (x3 - x2) * (y1 - y3));
    // b = ((y3 - y1) * (x - x3) + (x1 - x3) * (y - y3)) / ((y2 - y3) * (x1 - x3) + (x3 - x2) * (y1 - y3));
    // c = 1 - a - b;
    let divisor = ((y2 - y3) * (x1 - x3) + (x3 - x2) * (y1 - y3));
    let a = ((y2 - y3) * (x - x3) + (x3 - x2) * (y - y3)) / divisor;
    let b = ((y3 - y1) * (x - x3) + (x1 - x3) * (y - y3)) / divisor;
    let c = 1 - a - b;
    return a >= 0 && a <= 1 && b >= 0 && b <= 1 && c >= 0 && c <= 1;
}
let res = pointInTriangle(400, 300, 500, 100, 300, 300, 300, 200);

12、最长公共子序列 直接看原题吧 13、全排列 还是看原题吧

结语

就是这么朴实无华,这里就是能回想起得一些问题了,有更优解的话欢迎大家评论区补充~