KatInterview

Rank Top Earners Worldwide After Converting to USD

Heaps, Sorting & SearchAuto-graded · 17 test cases
EasyAuto-gradedHeaps, Sorting & Searchsortinghash-mapdata-manipulation

Last updated

Merge several lists of employee records, convert each salary to USD using a rate map, and return the top N earners with a deterministic tie-break.

A company keeps employee records in several separate files, one per country or region. Each file is a list of dictionaries. You must combine them and find the highest earners worldwide, with all pay expressed in US dollars.

Write a function that receives:

  • staff_files: a list of lists of dictionaries. Each dictionary is one employee and has the keys emp_id (int), name (str) and salary (number, in the employee's local currency). It also carries a currency indicator, described below.
  • fx_rates: a dictionary mapping a currency code to its USD rate, that is, how many US dollars one unit of that currency is worth.
  • top_n: how many employees to return (default 10).

Determining an employee's currency

For each employee, the currency code used to look up the rate is chosen like this:

  1. If the record has a "currency" key, use its value.
  2. Otherwise, if the record has a "country" key, use its value as the code.
  3. Otherwise, assume the code is "USD".

Task

  1. Concatenate all the files into one collection of employees. Every record from every file is kept, including records that look identical.
  2. For each employee, look up the rate for their currency code in fx_rates. If the code is not in fx_rates, the employee is skipped and does not appear in the result.
  3. Compute the USD salary as salary * rate, with the multiplication done in that order and with no rounding.
  4. Sort the employees by USD salary from highest to lowest. If two employees have equal USD salaries, the one with the smaller emp_id comes first.
  5. Return the first top_n entries. If top_n is zero or negative, return an empty list. If fewer than top_n employees qualify, return all of them.

Output format

Return a list of dictionaries, in ranked order, each with exactly these keys:

  • emp_id: the employee's id
  • name: the employee's name
  • salary_usd: the converted salary, computed as salary * rate

Constraints

  • The number of files and the number of employees per file may be zero.
  • emp_id values are integers; they may repeat across files.
  • Salaries and rates are non-negative numbers.
  • Do not round salary_usd; return the raw product.
  • Do not modify the input data.

Examples

Example 1

Input: rank_top_earners_usd([[{'emp_id': 1, 'name': 'Alice', 'salary': 120000, 'country': 'USD'}, {'emp_id': 2, 'name': 'Bob', 'salary': 95000, 'country': 'USD'}, {'emp_id': 3, 'name': 'Carol', 'salary': 115000, 'country': 'USD'}], [{'emp_id': 4, 'name': 'David', 'salary': 100000, 'currency': 'EUR'}, {'emp_id': 5, 'name': 'Eri', 'salary': 20000000, 'currency': 'JPY'}]], {'USD': 1, 'EUR': 1.12, 'JPY': 0.0091}, 10)

Output: [{'emp_id': 5, 'name': 'Eri', 'salary_usd': 182000}, {'emp_id': 1, 'name': 'Alice', 'salary_usd': 120000}, {'emp_id': 3, 'name': 'Carol', 'salary_usd': 115000}, {'emp_id': 4, 'name': 'David', 'salary_usd': 112000.00000000001}, {'emp_id': 2, 'name': 'Bob', 'salary_usd': 95000}]

Hints

Hint 1

Start by flattening all the files into one list. A currency can be resolved per record with a dictionary lookup that falls back to another key and then to a default.

Hint 2

Skip any record whose currency code has no entry in the rate map before you build the converted entries.

Hint 3

Sort with a composite key: negative USD salary first, then emp_id ascending. Finally slice with max(0, top_n) so that a negative limit gives an empty list.