Standardize Global Compensation and Rank Top Earners
SQL & DataAuto-graded · 16 test casesLast 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:
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').
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.
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_dateis on or beforeas_of_date. Rates withrate_date > as_of_datemust 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_usdin descending order. Each dict should contain:country_code(str)emp_id(int)name(str)salary_original(float or int): Equal to originalsalary.currency_code(str)salary_usd(float or int): Product ofsalaryand selectedrate_to_usd.
- Tie-Breaking: Use a stable sort order so that ties in
salary_usdretain their original order of processing.
Constraints:
1 <= len(employees) <= 50,0001 <= len(exchange_rates) <= 10,000as_of_dateis 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.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The problem requires aggregating, deduplicating, converting, and ranking global employee data.
-
Exchange Rate Resolution: Iterate through
exchange_ratesand filter out any entries whererate_date > as_of_date. Maintain a dictionary mapping eachcurrency_codeto its most recent valid rate. Since dates are formatted asYYYY-MM-DD, standard lexicographical string comparisons correctly match chronological order. When a strictly newer date for a currency is encountered, update the entry. -
Employee Deduplication: Iterate through the list of
employees. Use a composite key(country_code, emp_id)in a hash map to keep track of unique employees. Ignoring subsequent entries ensures that the first record encountered takes precedence. -
Salary Conversion: For each unique employee, look up their
currency_codein the valid rates mapping. If present, multiply the local compensation byrate_to_usdto obtainsalary_usd. If no valid exchange rate exists for that currency, exclude the employee from the output. -
Ranking: Sort the aggregated results by
salary_usdin descending order using Python's built-insort()(which is stable) and truncate to the first 10 items.
Complexity Analysis
- Time Complexity: $\mathcal{O}(R + E \log E)$, where $R$ is the number of exchange rate records and $E$ is the number of employee records.
- Processing exchange rates takes $\mathcal{O}(R)$ time.
- Deduplicating employee records takes $\mathcal{O}(E)$ time.
- Sorting converted records takes $\mathcal{O}(E \log E)$ time.
- Space Complexity: $\mathcal{O}(R + E)$ to store the rate lookup mapping and unique employee dictionaries.