Skip to content

面试过程中遇到的小题目

我印象里的第一道面试题 让我手写的题目

1.实现 在 AABBCCCDDDDDDD 记录每个字母出现的次数?

javascript
const words = "AABBBCCCDDDEEEAQQ____";
const getNumberLetter = (words) => {
  return words.split("").reduce((a, b) => {
    if (a.hasOwnProperty(b)) {
      return { [b]: a[b]++, ...a };
    } else {
      return { [b]: 1, ...a };
    }
  }, {});
};
javascript
const words = "AABBBCCCDDDEEEAQQ____";
const getNumberLetter = (words) => {
  const obj = {};
  for (let i of words) {
    if (obj.hasOwnProperty(i)) {
      obj[i]++;
    } else {
      obj[i] = 1;
    }
  }
  return obj;
};
javascript
const words = "AABBBCCCDDDEEEAQQ____";
const getNumberLetter = (words) => {
  const obj = {};
  for (let i = 0; i < words.length; i++) {
    if (obj.hasOwnProperty(words[i])) {
      obj[words[i]]++;
    } else {
      obj[words[i]] = 1;
    }
  }
  return obj;
};
javascript
const words = "AABBBCCCDDDEEEAQQ____";
const getNumberLetter = (words) => {
  const obj = {};
  words.split("").forEach((item) => {
    if (obj.hasOwnProperty(item)) {
      obj[item]++;
    } else {
      obj[item] = 1;
    }
  });
  return obj;
};
javascript
const words = "AABBBCCCDDDEEEAQQ____";
const getNumberLetter = (words) => {
  const obj = {};
  for (let i in words) {
    if (obj.hasOwnProperty(words[i])) {
      obj[words[i]]++;
    } else {
      obj[words[i]] = 1;
    }
  }
  return obj;
};
javascript
const words = "AABBBCCCDDDEEEAQQ____";
const getNumberLetter = (words) => {
  const obj = {};
  words.split("").map((item) => {
    if (obj.hasOwnProperty(item)) {
      obj[item]++;
    } else {
      obj[item] = 1;
    }
  });
  return obj;
};

2.数组扁平化

javascript
const arr = [1, [2, [3, [4, 5], 6], 7], 8];
const flat = (arr) => arr.flat(Infinity);
javascript
const arr = [1, [2, [3, [4, 5], 6], 7], 8];
const flat = (arr, aa) =>
  arr.reduce((a, b) => {
    if (Array.isArray(b)) {
      return flat(b, a);
    } else {
      return [...a, b];
    }
  }, aa);
javascript
const arr = [1, [2, [3, [4, 5], 6], 7], 8];
const flat = (arr) => {
  const result = [];
  arr.forEach((item) => {
    if (Array.isArray(item)) {
      result.push(...flat(item));
    } else {
      result.push(item);
    }
  });
  return result;
};
javascript
const arr = [1, [2, [3, [4, 5], 6], 7], 8];
const flat = (arr, result) => {
  if (!result) {
    result = [];
  }
  return arr.map((item) => {
    if (Array.isArray(item)) {
      return flat(item, result);
    } else {
      return result.push(item);
    }
  });
};
javascript
const arr = [1, [2, [3, [4, 5], 6], 7], 8];
const flat = (arr, result) => {
  arr.forEach((item) => {
    if (Array.isArray(item)) {
      return flat(item, result);
    } else {
      result.push(item);
    }
  });
  return result;
};
javascript
const arr = [1, [2, [3, [4, 5], 6], 7], 8];
const flat = (arr) => {
  return JSON.parse(
    "[" +
      JSON.stringify(arr).replaceAll("[", "").replaceAll("]", "").split(",") +
      "]"
  );
};

3.数组去重

javascript
const arr = [1, 3, 4, 3, 4, 5];
let arrayDeDuplication = [...new Set(arr)];
javascript
const arrayDeDuplication = (arr) => {
  let result = [];
  arr.forEach((item) => {
    if (!result.includes(item)) {
      result.push(item);
    }
  });
  return result;
};
javascript
const arrayDeDuplication = (arr) => {
  let result = [];
  arr.forEach((item) => {
    if (result.indexOf(item) === -1) {
      result.push(item);
    }
  });
};
javascript
const arrayDeDuplication = (arr) => {
  let result = [];
  arr.forEach((item) => {
    let state = result.filter((it) => it === item);
    if (state.length === 0) {
      result.push(item);
    }
  });
  return result;
};
markdown
主要思路大差不差

- 首先遍历数组
- 判断当前元素是否存在于数组中
- 存在则进入下依次循环 不存在则添加到数组中

4.伪数组转化为数组

javascript
const pseudoArray = [];
Array.form(pseudoArray);
javascript
javascript
javascript

5.debounce(防抖)

6.throttle(节流)

7.函数珂里化

函数接受多个参数为函数的变量

8.继承

javascript
javascript

9.深浅拷贝

10.promise 源码实现思路 实现 all race any 并行方式

11.JSONP

12.图片懒加载

13.滚动加载

14.渲染大量数据 不卡主页面

15.将 VirtualDom 转化为真实 DOM 结构

16.new 操作符

17.字符串 abcde 如何反转

1 . 首先字符串转成数组

  • Array.from()
  • [...new Set()]
  • split
javascript
let str = "helloworld";
let tt = [...str].reduce((a, b) => b + a, "");
javascript
let str = "helloworld";
[...str]
  .reduce((a, b) => {
    a.unshift(b);
    return a;
  }, [])
  .join("");

18.实现将树结构替换字段

javascript
function replaceTreeField(obj, newFiled, oldFiled) {
  for (const key in obj) {
    if (key === oldFiled) {
      obj[newFiled] = obj[key];
      delete obj[key];
    }
    if (obj.hasOwnProperty("children")) {
      obj.children.forEach((child) => {
        replaceWwField(child, newFiled, oldFiled);
      });
    }
  }
}
javascript
function replaceTreeField(obj, newFiled, oldFiled){

}

https://blog.csdn.net/Ed7zgeE9X/article/details/119336745


### 19.将下面的代码补全

```js
const a = (cb) => {
  setTimeout(() => {
    cb(Math.random());
  }, 1000);
};
const b = () => {
  return new Promise((resolve) => {
    a(resolve);
  });
};
b().then((res) => {
  // res是函数a执行后,函数a的参数cb,会接收到的参数
  console.log(res);
});

20 实现 add 函数

js
// 实现如下逻辑
add(1, 2, 3)(4, 5)(); // 15
add(1, 2)(4)(); // 7
const a1 = add(1);
const a2 = a1(1)(); // 2
const a3 = a1(2)(); // 3
js
function add(...args) {
  let numbers = args;

  function _add(...rest) {
    if (rest.length === 0) {
      return numbers.reduce((sum, num) => sum + num, 0);
    } else {
      // 创建新的闭包保存所有参数
      const newNumbers = [...numbers, ...rest];
      return add(...newNumbers);
    }
  }

  return _add;
}

console.log(add(1, 2, 3)(4, 5)()); // 15
add(1, 2)(4)(); // 7
const a1 = add(1);
const a2 = a1(1)(); // 2
const a3 = a1(2)(); // 3

console.log(a2);
console.log(a3);

21 实现 arr2tree 函数

js
const arr = [
  { id: 1, name: "部门1", pid: 0 },
  { id: 2, name: "部门2", pid: 1 },
  { id: 3, name: "部门3", pid: 1 },
  { id: 4, name: "部门4", pid: 3 },
  { id: 5, name: "部门5", pid: 4 },
];

function arr2Tree(arr, pid = 0) {
  return arr
    .filter((_) => _.pid === pid)
    .map((_) => {
      return {
        ..._,
        children: arr2Tree(arr, _.id),
      };
    });
}

const tree = arr2Tree(arr);
console.log(tree);

22 实现 tree2arr 函数

const tree = [
  {
    id: 1,
    name: "部门1",
    pid: 0,
    children: [
      {
        id: 2,
        name: "部门2",
        pid: 1,
        children: [],
      },
      {
        id: 3,
        name: "部门3",
        pid: 1,
        children: [
          {
            id: 4,
            name: "部门4",
            pid: 3,
            children: [
              {
                id: 5,
                name: "部门5",
                pid: 4,
                children: [],
              },
            ],
          },
        ],
      },
    ],
  },
];

function tree2arr(tree) {
  let arr = [];

  function _tree2arr(tree) {
    tree.forEach((_) => {
      if (_.children && _.children.length > 0) {
        _tree2arr(_.children);
      }
      const { children, ...rest } = _;
      arr.push(rest);
    });
  }
  _tree2arr(tree);
  return arr;
}

const arr = tree2arr(tree);
console.log(arr);

23 给数组排序对象按 某个值排序

js
const arr = [
  { id: 2, name: "部门2", pid: 1 },
  { id: 5, name: "部门5", pid: 4 },
  { id: 4, name: "部门4", pid: 3 },
  { id: 3, name: "部门3", pid: 1 },
  { id: 1, name: "部门1", pid: 0 },
];

function order(arr, field = "pid", orderBy = "asc") {
  return arr.sort((a, b) => {
    if (a[field] < b[field]) {
      return orderBy === "asc" ? -1 : 1;
    }
    if (a[field] > b[field]) {
      return orderBy === "asc" ? 1 : -1;
    }
    return 0;
  });
}
console.log(order(arr));

24 列举你知道的排序方式

常见的排序算法

  1. 冒泡排序 (Bubble Sort)
    相邻元素比较交换,逐步将最大元素"冒泡"到末尾

  2. 选择排序 (Selection Sort)
    每次选择最小/最大元素放到已排序序列末尾

  3. 插入排序 (Insertion Sort)
    将未排序元素插入到已排序序列的适当位置

  4. 快速排序 (Quick Sort)
    分治法,选取基准值将数组分为两部分递归排序

  5. 归并排序 (Merge Sort)
    分治法,将数组分成最小单元后两两合并排序

  6. 堆排序 (Heap Sort)
    利用堆数据结构进行选择排序

  7. 希尔排序 (Shell Sort)
    改进的插入排序,按增量分组进行排序

  8. 计数排序 (Counting Sort)
    非比较排序,适用于整数小范围数据

  9. 桶排序 (Bucket Sort)
    将数据分到有限数量的桶里分别排序

  10. 基数排序 (Radix Sort)
    按位进行排序,从低位到高位依次比较

关注公众号