xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Node.js 面试题 All In One

Node.js 面试题 All In One

Node.js 应用领域

BFF

CLI

前端基建工具库

爬虫

BFF

Pattern: Backends For Frontends

用于前端的后端模式

https://samnewman.io/patterns/architectural/bff/

https://learn.microsoft.com/zh-cn/azure/architecture/patterns/backends-for-frontends

Node.js 面试题

手写 koa compose

第 75 题:数组里面有10万个数据,取第一个元素和第10万个元素的时间相差多少


/*

// ✅ > 100000
2**64;
18446744073709552000

// ✅ > 100000
2**32;
4294967296

// ❌ 太小了 < 100000
2**16;
65536

// ❌ 太小了 < 100000
2**8;
256

*/

image

Version 107.0.5300.0 (Official Build) dev (x86_64)

const arr = (new Uint32Array(100000)).map((item, i) => i +  1);

function testPerformance(arr, i) {
  let startTime = performance.now();
  console.log(`arr[i], i =`, arr[i], i);
  let endTime = performance.now();
  console.log(`🚀performance time is \`${endTime - startTime}\` ms`);
}

function testTime(arr, i) {
  console.time('array test');
  console.log(`arr[i], i =`, arr[i], i);
  console.timeEnd('array test');
}

testPerformance(arr, 0);
testPerformance(arr, 99999);

console.log('\n');

testTime(arr, 0);
testTime(arr, 99999);


https://github.com/Advanced-Frontend/Daily-Interview-Question/issues/124#issuecomment-1255211384

https://muyiy.cn/question/js/75.html

https://lqk9511.github.io/blog/interview/question/74.html

第 67 题:随机生成一个长度为 10 的整数类型的数组,例如 [2, 10, 3, 4, 5, 11, 10, 11, 20],将其排列成一个新数组,要求新数组形式如下,例如 [[2, 3, 4, 5], [10, 11], [20]]。


/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2022-09-25
 * @modified
 *
 * @description
 * @difficulty
 * @ime_complexity O(n)
 * @space_complexity O(n)
 * @augments
 * @example
 * @link https://leetcode.com/problems//
 * @solutions
 *
 * @best_solutions
 *
 */

export {};

const log = console.log;



// 第 67 题:随机生成一个长度为 10 的整数类型的数组,例如 [2, 10, 3, 4, 5, 11, 10, 11, 20],将其排列成一个新数组,要求新数组形式如下,例如 [[2, 3, 4, 5], [10, 11], [20]]。

// ??? 随机数组的整数范围限制 ?

// 排序,分组, 分几组??? 4: 2: 1

// ??? 0~9, 10 ~ 19, 20 ~ 29 ???


// ??? 重复数字如何处理, 去重


const autoGroup = (arr: number[]) => {
  let result: number[][] = [];
  const temp = arr.sort((a, b) => a - b > 0 ? 1 : -1);
  // const max = temp[arr.length - 1];
  const max = Math.max(...arr);
  const groups = Math.ceil(max / 10);
  let index = 0;
  let tempMax = temp[0] - 1;
  while(index <= groups) {
    index += 1;
    const group: number[] = [];
    let i = 0;
    if(temp.indexOf(tempMax) > -1) {
      // 优化:减少遍历的次数
      i = temp.indexOf(tempMax);
    }
    for (i; i < temp.length; i++) {
      const item = temp[i];
      if(item > tempMax && item < index * 10) {
        group.push(item);
      }
    }
    tempMax = group[group.length - 1];
    result.push(group);
  }
  return result;
}

const result = autoGroup([2, 10, 3, 4, 5, 11, 10, 11, 20]);

console.log(`groups result =`,  result);
// [[2, 3, 4, 5], [10, 11], [20]]


      

https://github.com/Advanced-Frontend/Daily-Interview-Question/issues/113

refs

https://www.cnblogs.com/xgqfrms/p/16728838.html

https://www.runoob.com/nodejs/nodejs-repl.html



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2022-09-21 12:15  xgqfrms  阅读(126)  评论(4编辑  收藏  举报