All problems

Top Earners in Each Department

hardPythonGroupByRanking

Bright Harbor's compensation review comes round every quarter, and HR wants the same summary each time: for every department, who are its best-paid people? How many names they want back changes with what the review is for — one apiece for a board slide, a handful for a pay-band audit — so the count is decided on the day and the report has to take it as it comes.

The obvious version of this report — order everyone in the company by salary and take the top few — is the one they had before, and it was useless. The departments are wildly different sizes and pay on completely different scales, so a company-wide list was Engineering and Data all the way down, and Marketing never appeared on it once. The best-paid people in Marketing are still the best-paid people in Marketing, however modest the numbers look next to Engineering's.

employees_df — one row per current employee. salary is annual pay in dollars, and manager is blank for people who report to nobody.

id name department salary hire_date manager
1 Ava Chen Engineering 145000 2021-03-14
2 Ben Ortiz Engineering 118000 2022-06-01 Ava Chen
3 Cara Novak Engineering 121000 2023-01-10 Ava Chen
4 Deshawn Lee Sales 95000 2020-09-23
5 Elin Kask Sales 88000 2022-11-05 Deshawn Lee
6 Farid Amiri Marketing 76000 2023-04-18
7 Grace Kim Data 132000 2021-07-30
8 Hugo Silva Data 110000 2023-02-14 Grace Kim
9 Ines Duarte Data 104000 2023-08-01 Grace Kim
10 Jonas Weber Sales 91000 2021-12-19 Deshawn Lee

Input

The DataFrame above is already built for you. One line of input arrives: a whole number, how many names HR wants from each department this quarter. It is at least 1.

Task: Print a dict keyed by department name, with the departments in alphabetical order. Each value is a list of the names of that department's highest-paid people, as many as were asked for, better-paid first. A department holding fewer people than that gets a correspondingly shorter list — every name it has, in order — rather than being padded or dropped.

Example: if two names had been asked for, the printed dict would look like {'Legal': ['Bigger Earner', 'Smaller Earner'], 'Facilities': ['Only Person There']}.

Sign in to solve this problem

Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.

Sign in

Discussion

Sign in to join the discussion — reading is open to everyone.

Loading comments…