面试复盘 字节跳动-抖音电商前端(商家平台)-一面

1,383 阅读2分钟

面试复盘


字节跳动-抖音电商前端(商家平台)-一面

  1. 自我介绍,根据简历聊项目,,项目中的亮点,难点,难点怎么解决的
  2. vue 和 React 在使用和实现上的异同
  3. 什么是虚拟 dom
  4. Promise 用过哪些 API,Promise.all, Promise.race, 传进什么类型的参数,输出什么结果,输出结果是有没有顺序,什么顺序,如果其中有一个 reject 之后输出什么(谢谢面试官没让我实现一个 promise.all)
  5. 说一下 EventLoop&宏任务&微任务,然后回答下面的代码输出什么
  setTimeout(function() {
    setTimeout(function() {
    console.log(1)
  }, 100)

  console.log(2)

  setTimeout(function() {
    console.log(3)
    }, 0)
  }, 0)

  setTimeout(function () {
    console.log(4)
  }, 100)

  console.log(5)
  1. 写个算法题:给定一个二叉树和一个数字 n,判断二叉树中是否有一个路径上的数字之和等于给定的数字 n
(1). 递归
function hasPathSum(root, sum) {
   if(!root) {
       return false
   }
   if(!root.left && !root.right) {
       return sum === root.val
   }
   return hasPathSum(root.left,sum - root.val) || hasPathSum(root.right,sum-root.val)
}
时间复杂度O(N):其中 N 是树的节点数。对每个节点访问一次。
空间复杂度O(H):其中 HH 是树的高度。空间复杂度主要取决于递归时栈空间的开销,最坏情况下,树呈现链状,空间复杂度为 O(N)。平均情况下树的高度与节点数的对数正相关,空间复杂度为 O(logN)。

(2). 队列或栈
var hasPathSum = function(root, targetSum) {
  if(root == null){
    return false;
  }
  var queue1 = [root];
  var queue2 = [root.val];
  while(queue1.length !== 0){
    var node = queue1.shift();
    var rootVal = queue2.shift();
    if(node.left == null && node.right == null && rootVal == targetSum){
      return true;
    }
    if(node.left){
      queue1.push(node.left);
      queue2.push(node.left.val + rootVal);
      }
    if(node.right){
      queue1.push(node.right);
      ueue2.push(node.right.val + > rootVal);
    }
  }
  return false;
};
时间复杂度O(N):其中 N 是树的节点数。对每个节点访问一次。
空间复杂度O(N):其中 N 是树的节点数。空间复杂度主要取决于队列的开销,队列中的元素个数不会超过树的节点数。
  • 实现防抖和节流
function debounce(func, wait, immediate) {
  let timer;
  return function() {
      const context = this;
      const args = arguments;
      if(timer) clearTimeout(timer)
      if(immediate) {
          const callNow = !timer;
          timer = setTimeout(() => {
              timer = null;
          },wait);
      if(callNow) func.apply(context,args);
      }else {
        timer = setTimeout(() => {
            func.apply(context,args);
        },wait)
      }
  }
}
function throttle(func,wait) {
  let previous = 0;
  return function() {
      const now = Date.now();
      const context = this;
      const args = arguments;
      if(now - previous > wait) {
          function.apply(context, args);
          previous = now;
      }
  }
}

关于this,arguments的指向问题(es6+ 环境可以用箭头函数进行避免) 在执行debounce(func,wait)&throttle(func,wait)时,会返回一个新的匿名函数作为返回值,当事件触发时,例如content.onclick() ,函数内部的this指向content,如果在返回的匿名函数内部直接执行func(),func中的this会指向window,很容易出错

  1. 状态管理解决方案(vuex,redux,dva)管理实现流程,内部实现方式(发布订阅模式),写一个简单的EventBus的实现