← Problems82. Median salary per departmentMediumPySpark
00:00 / 20:00

Median salary per department

Medium100% solve itAsked at Adobe, Deloitte, Microsoft

An average salary is pulled around by one very large one. Compensation wants the median instead.

Input

employees emp_id bigint dept string salary double -- null where no salary has been recorded

Output

One row per dept, with the columns:

  • dept
  • headcount — every employee in the department, salary recorded or not
  • median_salary — the exact median of the recorded salaries, rounded to 2 decimal places, or null if none were recorded

For an even number of recorded salaries the median is the mean of the two middle ones.

Example

deptsalary
OPS10.0
OPS20.0
deptheadcountmedian_salary
OPS215.0

15.0 is the answer even though nobody earns it.

Constraints

  • Exact, not approximate. An approximate median returns a value somebody actually earns, which for an even-sized department is the lower of the two middle salaries and not the median.
  • An employee with no salary recorded is still in the headcount.
  • Row order is not graded. Column names and their order are.

Example 1an odd department and an even one

Input · employees
deptemp_idsalary
ENG1100
ENG2200
ENG3300
OPS410
OPS520
Output
deptheadcountmedian_salary
ENG3200
OPS215

Example 2one employee is their own median

Input · employees
deptemp_idsalary
LEGAL175.5
SALES210
SALES330
SALES450
SALES570
Output
deptheadcountmedian_salary
LEGAL175.5
SALES440

Topics

aggregation

Similar problems

Community-reported interview topic. Not an official company question and no affiliation is implied.

solution.py
Loading editor…
Draft not saved yet · Spaces 4 · UTF-8⌘↵ run ⌘⇧↵ submit
Nothing run yet

Run against the public tests, or submit to score against all of them.