KatInterview

Standardize Global Compensation and Rank Top Earners

SQL & DataAuto-graded · 16 test cases
MediumAuto-gradedSQL & Datadata-manipulationhash-mapsorting

Last updated

Deduplicate employee records across regions, convert local salaries to USD using historical exchange rates, and rank the top earners.

Given global workforce data and historical currency conversion tables, your goal is to determine the top 10 highest-paid workers in USD as of a target date.

You are provided with:

  1. employees: A list of dictionaries representing individual worker records with keys:
    • emp_id (int): Unique ID within a country.
    • name (str): Worker's full name.
    • salary (float or int): Compensation in local currency.
    • currency_code (str): ISO 3-letter currency code (e.g., 'USD', 'GBP').
    • country_code (str): ISO 2-letter country code (e.g., 'US', 'GB').
  2. exchange_rates: A list of dictionaries containing currency conversion details:
    • currency_code (str): ISO currency code.
    • rate_to_usd (float): Conversion factor to USD.
    • rate_date (str): Date of rate publication in 'YYYY-MM-DD' format.
  3. as_of_date (str): Cut-off date string in 'YYYY-MM-DD' format.

Rules and Requirements:

  • Deduplication: Individual records must be uniquely identified by the composite key (country_code, emp_id). If the same key appears multiple times, preserve only the first occurrence.
  • Exchange Rates: For each currency, select the single most recent exchange rate whose rate_date is on or before as_of_date. Rates with rate_date > as_of_date must be ignored. If multiple entries for a currency share the latest valid date, keep the one that appears first.
  • Missing Rates: If an employee's currency has no valid rate on or before as_of_date, exclude that employee from the final results.
  • Conversion: Compute the USD equivalent salary as salary * rate_to_usd.
  • Output Format: Return a list of up to 10 dictionaries sorted by salary_usd in descending order. Each dict should contain:
    • country_code (str)
    • emp_id (int)
    • name (str)
    • salary_original (float or int): Equal to original salary.
    • currency_code (str)
    • salary_usd (float or int): Product of salary and selected rate_to_usd.
  • Tie-Breaking: Use a stable sort order so that ties in salary_usd retain their original order of processing.

Constraints:

  • 1 <= len(employees) <= 50,000
  • 1 <= len(exchange_rates) <= 10,000
  • as_of_date is always a valid ISO date formatted as 'YYYY-MM-DD'.

Examples

Example 1

Input: top_employees_by_salary_usd([{'emp_id': 1001, 'name': 'Alice', 'salary': 120000, 'currency_code': 'USD', 'country_code': 'US'}, {'emp_id': 1002, 'name': 'Bob', 'salary': 90000, 'currency_code': 'USD', 'country_code': 'US'}, {'emp_id': 2001, 'name': 'Claire', 'salary': 80000, 'currency_code': 'GBP', 'country_code': 'GB'}, {'emp_id': 2002, 'name': 'Dan', 'salary': 95000, 'currency_code': 'GBP', 'country_code': 'GB'}, {'emp_id': 3001, 'name': 'Emi', 'salary': 12000000, 'currency_code': 'JPY', 'country_code': 'JP'}, {'emp_id': 3002, 'name': 'Fumi', 'salary': 8500000, 'currency_code': 'JPY', 'country_code': 'JP'}], [{'currency_code': 'USD', 'rate_to_usd': 1, 'rate_date': '2025-08-31'}, {'currency_code': 'GBP', 'rate_to_usd': 1.27, 'rate_date': '2025-08-31'}, {'currency_code': 'JPY', 'rate_to_usd': 0.0068, 'rate_date': '2025-08-31'}], '2025-08-31')

Output: [{'country_code': 'GB', 'emp_id': 2002, 'name': 'Dan', 'salary_original': 95000, 'currency_code': 'GBP', 'salary_usd': 120650}, {'country_code': 'US', 'emp_id': 1001, 'name': 'Alice', 'salary_original': 120000, 'currency_code': 'USD', 'salary_usd': 120000}, {'country_code': 'GB', 'emp_id': 2001, 'name': 'Claire', 'salary_original': 80000, 'currency_code': 'GBP', 'salary_usd': 101600}, {'country_code': 'US', 'emp_id': 1002, 'name': 'Bob', 'salary_original': 90000, 'currency_code': 'USD', 'salary_usd': 90000}, {'country_code': 'JP', 'emp_id': 3001, 'name': 'Emi', 'salary_original': 12000000, 'currency_code': 'JPY', 'salary_usd': 81600}, {'country_code': 'JP', 'emp_id': 3002, 'name': 'Fumi', 'salary_original': 8500000, 'currency_code': 'JPY', 'salary_usd': 57800}]

Hints

Hint 1

Filter exchange rates so that rate_date <= as_of_date before selecting the latest exchange rate per currency.

Hint 2

Use a hash map indexed by (country_code, emp_id) to deduplicate employees while preserving the first instance seen.

Hint 3

Convert salaries after resolving valid exchange rates, filter out records with missing conversion rates, then perform a stable sort.