TikTok VO Coding Interview: Build an Employee List with Selected Titles

16 Views
No Comments

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));

This problem asks you to enrich an employee list with a title based on a mapping from title names to one id or many ids, while keeping the input arrays and objects immutable. A clean solution is to first normalize selectedTitles into an id-to-title lookup, then map over employees and build a new result array. If a second mapping such as priorTitles is provided, you can compare the current and previous titles to include only employees whose title changed. This is a straightforward hash-map and array-transform interview problem.

END
 0