All problems

Nth Highest Salary per Department

mediumPythonGroupByRanking

You're a data analyst at Kestrel Robotics, and the pay committee is redrawing the salary bands. They work down each department one place at a time. The biggest salary usually belongs to the person running the team and is agreed at board level, so the committee often throws it out and looks at the place below it — the top of the staff band. Some weeks they want the place below that instead. Which place they are looking at changes from meeting to meeting, so it is a setting on the tool rather than something built into it.

That is an awkward thing to ask a table for, and it is not always available: Marketing is one person, so there is nothing below its top salary to report.

employees_df — one row per employee. salary is annual, in dollars. manager is None for anyone who reports to nobody in this table.

id name department salary hire_date manager
1 Ava Chen Engineering 145000 2021-03-14 None
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 None
5 Elin Kask Sales 88000 2022-11-05 Deshawn Lee
6 Farid Amiri Marketing 76000 2023-04-18 None
7 Grace Kim Data 132000 2021-07-30 None
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 — the employee rows are not read from input. What does arrive is a single line holding a whole number: the place in the band the committee is meeting about, counted down from the top, where 1 means a department's highest salary, 2 the salary below it, and so on.

Task: Print a dict mapping department name to the salary sitting at the place that arrives, as a plain integer, with the departments in alphabetical order. A department with fewer employees than the place asked for has no such salary and must not appear in the dict at all. Places are counted by position, not by distinct value: if two people share the top salary, the place below it is that same number again.

Example: if only two departments qualified, you'd print {'Legal': 70000, 'Ops': 65000}.

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…