TikTok VO 面试真题解析:构建员工列表与选中职位映射

29次阅读
没有评论

Given an array of data, where each entry is an object with id and value keys, and an object of ids, where each key is the name and value is either a single id or an array of ids. Create an array of employees with id, value, and their selected title.

Do not mutate the arguments passed in (selectedTitles and employees).

@param {Record<string, number | number[]>} selectedTitles
@param {Array<{id: number, value: string}>} employees
@returns {Array<{id: number, value: string, title: string}>}

const employeesWithTitles = (selectedTitles, employees) => {
}

const selectedTitles = {engineer: [43, 5, 61],
  productManager: 3,
  manager: 10,
}

const employees = [{ id: 5, value: 'Miles'},
  {id: 61, value: 'Francis'},
  {id: 91, value: 'Tanmay'},
  {id: 3, value: 'Ataur'},
  {id: 10, value: 'Nan'},
  {id: 43, value: 'Andrew'},
  {id: 30, value: 'Xu'}
]

console.log(employeesWithTitles(selectedTitles, employees));

这道题要求根据“职位名 -> 员工 id 或 id 数组”的映射,在员工列表中补充每个人对应的 title,并且不能修改原始输入。核心做法是先把 selectedTitles 展平成“id -> title”的查找表,再遍历 employees 生成新数组。这样可以用哈希表把查找从多重循环优化到线性时间;如果题目还要求比较前后 title 是否变化,只需再维护一份 priorTitles,并在 title 未变化时跳过输出或按要求过滤即可。

正文完
 0