Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
tingyuan committed Aug 21, 2024
1 parent 9127d63 commit 855311e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 5 deletions.
71 changes: 67 additions & 4 deletions blogs/interview/coding.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,29 @@
```js
function concurrentRequests(urls, limit) {
return new Promise((resolve, reject) => {
const results = new Array(urls.length)
const results = Array(urls.length).fill()
let completedCount = 0
let index = 0
let fetchingCount = 0

function processNext() {
if (index >= urls.length) return

if (index >= urls.length || fetchingCount >= limit) {
return
}
const currentIndex = index++
fetchingCount++
window
.fetch(urls[currentIndex])
.then((response) => response.json())
.then((data) => {
results[currentIndex] = data
})
.catch((error) => {
results[currentIndex] = { error: error.message }
results[currentIndex] = { error }
})
.finally(() => {
completedCount++
fetchingCount--
if (completedCount === urls.length) {
resolve(results)
} else {
Expand Down Expand Up @@ -70,3 +74,62 @@ function flatArray(array, depth = 1) {
```

:::

3. [最长有效括号的长度](https://leetcode.cn/problems/longest-valid-parentheses/description/)

给你一个只包含 '(' 和 ')' 的字符串,找出最长有效(格式正确且连续)括号子串的长度。

::: detail result

```js
var longestValidParentheses = function (s) {
const stack = [-1] // 存储当前有效括号上一个位置的下标
let i = 0
let maxLen = 0
while (i < s.length) {
// 遇到合法的括号就弹出,同时计算最长长度
if (s[i] === ')' && s[stack.at(-1)] === '(') {
stack.pop()
// 此时stack最顶部的位置表示该位置之后都是合法的
maxLen = Math.max(maxLen, i - stack.at(-1))
} else {
stack.push(i)
}
i++
}
return maxLen
}
```

:::

4. 将一维数组转换为树形结构的数据

::: detail result

```js
function arrayToTree(data) {
const idMap = new Map()
const root = []

// 创建 id 到节点的映射
for (const item of data) {
idMap.set(item.id, { ...item, children: [] })
}

// 构建树形结构
for (const item of data) {
const node = idMap.get(item.id)
if (item.parentId === '') {
root.push(node)
} else {
const parent = idMap.get(item.parentId)
parent.children.push(node)
}
}

return root
}
```

:::
3 changes: 2 additions & 1 deletion blogs/interview/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ function advancedRequest(url, timeout, errorRetry) {

:::

6. 说一下JavaScript中能使代码运行阻塞的一些情况吗

## http

1. 当前网站 A 向第三方网站 C(可信但是不同域名)请求一个接口并且希望带上二者的 cookie,请问需要满足什么条件或者做什么设置?
Expand Down Expand Up @@ -234,7 +236,6 @@ function advancedRequest(url, timeout, errorRetry) {
## 算法

1. 判断一个二叉树是否是另一个二叉树的**子结构** [leetcode](https://leetcode-cn.com/problems/shu-de-zi-jie-gou-lcof/)

2. 使用递归和迭代两种方式反转一个链表 [leetcode](https://leetcode-cn.com/problems/reverse-linked-list/)
3. 有一个数组,从其中找到两个数的最大差值(要求这两个数递增),返回对应的差值
4. 使用两种方法实现 N 叉树的层次遍历 [leetcode](https://leetcode.cn/problems/n-ary-tree-level-order-traversal/)
Expand Down

0 comments on commit 855311e

Please sign in to comment.