字节4.8前端三面
自我介绍
有挑战的项目(听了之后觉得没什么好聊的就跳过了)
vite的原理
vite有什么特点
如果要实现一个axios库你会怎么实现
vuex的mutation为什么不能处理异步任务
手撕最大并行异步任务(我感觉写出来可能要很久,就说说思路)
/* 标题 并发请求限制 题目描述 【背景】 一般浏览器会限制并发请求数,微信小程序之前也限制过最多请求不超过10个。 现在,让我们来实现一下这个功能。 【问题描述】 实现这样一个函数scheduler,函数入参为并发最大次数。 如下最终输出顺序: 2、3、 1、 4 一开始,1、2两个任务进入队列 500ms时,2完成,输出2,任务3进队 800ms时,3完成,输出3,任务4进队 1000ms时,1完成,输出1 1200ms时,4完成,输出4 */ // -----------------mock一些请求 const request1 = () => new Promise((resolve, reject) => { setTimeout(() => { resolve(1); }, 1000); }); const request2 = () => new Promise((resolve, reject) => { setTimeout(() => { resolve(2); }, 500); }); const request3 = () => new Promise((resolve, reject) => { setTimeout(() => { resolve(3); }, 300); }); const request4 = () => new Promise((resolve, reject) => { setTimeout(() => { resolve(4); }, 400); }); // -----------------最多并发2个请求 function scheduler(max) { // ------你的代码 } const addRequest = scheduler(2); addRequest(request1).then(res => { console.log(res); }); addRequest(request2).then(res => { console.log(res); }); addRequest(request3).then(res => { console.log(res); }); addRequest(request4).then(res => { console.log(res); });
自己的写法和参考牛友绿豆沙的做法(async/await写法),写了以下解答:
function scheduler(max) { // 最大并行数为 10 max = max > 10 ? 10 : max; // 用 async/await 语法实现不定数量异步任务串行执行 let count = 0; const queue = []; async function _addReq(reqf) { count >= max ? await new Promise((resolve, reject) => { queue.push(resolve); }) : ""; count++; const result = await reqf(); count--; if (queue.length) { queue.shift()(); } return result; } return _addReq; // 构造 promise 链实现不定数量异步任务串行执行 // const path = []; // const queue = []; // function _addReq(reqf) { // return new Promise((resolve, reject) => { // if (path.length < max) { // path.push( // reqf().then((res) => { // resolve(res); // }) // ); // } else { // queue.push(reqf); // for (let i = 0; i < path.length; i++) { // path[i] = path[i].then(() => { // if (queue.length) { // const rf = queue.shift(); // return rf().then((res) => { // resolve(res); // }); // } // return; // }); // } // } // }); // } // return _addReq; }
手撕股票买卖问题,是leetcode121和122,先让你通过一次买卖找出最大利润,再让你通过不限买卖次数找出最大利润(没写出来)
最后事件差不多了,面试官直接跟我说很遗憾我没能写出最后两道题(淦呐,直接告诉我我不过是吧)
最后反问问了一下面试官对低代码这个东西的看法,然后就下线了
心累,倒在了算法题上
#字节跳动##春招##面经##前端#