Average salary per department for active employees at or above a salary threshold

Quick Overview

Given a list of employee records, keep only active employees whose salary meets a threshold, group them by department, and return each department's average salary rounded down in alphabetical order. It tests filtering, hash map grouping, integer aggregation and sorting.

Average salary per department for active employees at or above a salary threshold

Company: Reddit

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given an array of employee records where each record contains a `name`, `department`, `salary`, and `active` status. Perform this series of array transformations: - Filter out all inactive employees (where `active` is `0`). - From the remaining active employees, keep only those whose `salary` is greater than or equal to the given `threshold`. - Group the filtered employees by `department`. - For each department, compute the average salary, rounded down to the nearest integer. - Return a list of `[department, average_salary]` pairs sorted alphabetically by department name. Each employee is represented as a list `[name, department, salary, active]` where `name` is a string, `department` is a string, `salary` is an integer, and `active` is `1` (true) or `0` (false). ### Function Signature ```python def filter_and_group(employees: list[list], threshold: int) -> list[list]: ``` ### Rules - Each record is `[name, department, salary, active]`. The `name` field does not affect the result. - Department names are compared exactly as given, so the comparison is case-sensitive. - The average for a department is the floor of its salary total divided by its number of qualifying employees. - Sort the pairs in ascending order of department name using plain string comparison (character code order, which is Python's default string ordering). Each department appears once, so the order is unique. - A department with no qualifying employee does not appear in the output. If no employee qualifies, return an empty list. ### Constraints - `1 <= employees.length <= 10^4` - `1 <= len(name) <= 50` - `1 <= len(department) <= 50` - `1 <= salary <= 10^6` - `active` is `0` or `1` - `1 <= threshold <= 10^6` ### Examples ```text Input: employees = [["Alice","Engineering",90000,1],["Bob","Engineering",80000,1],["Charlie","Marketing",70000,1],["Diana","Marketing",60000,0],["Eve","Sales",50000,1],["Frank","Sales",85000,1]], threshold = 65000 Output: [["Engineering",85000],["Marketing",70000],["Sales",85000]] ``` Diana is inactive and Eve's salary of 50000 is below 65000, so both are removed. Engineering averages (90000 + 80000) // 2 = 85000, while Marketing keeps only Charlie (70000) and Sales keeps only Frank (85000). ```text Input: employees = [["Zara","HR",55000,1],["Yuki","HR",45000,1],["Xander","HR",60000,0]], threshold = 40000 Output: [["HR",50000]] ``` Xander is inactive. Zara (55000) and Yuki (45000) both meet the threshold, so HR averages (55000 + 45000) // 2 = 50000. ```text Input: employees = [["Ann","Ops",50001,1],["Ben","Ops",50000,1],["Cy","Ads",70000,0]], threshold = 50000 Output: [["Ops",50000]] ``` Ben's salary equals the threshold, so he stays. The Ops total is 100001 over 2 employees, which is 50000.5 and rounds down to 50000. Ads has no active employee, so it does not appear.

Overview: Given a list of employee records, keep only active employees whose salary meets a threshold, group them by department, and return each department's average salary rounded down in alphabetical order. It tests filtering, hash map grouping, integer aggregation and sorting.

Reddit logo
Reddit
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given an array of employee records where each record contains a name, department, salary, and active status. Perform this series of array transformations:

  • Filter out all inactive employees (where active is 0 ).
  • From the remaining active employees, keep only those whose salary is greater than or equal to the given threshold .
  • Group the filtered employees by department .
  • For each department, compute the average salary, rounded down to the nearest integer.
  • Return a list of [department, average_salary] pairs sorted alphabetically by department name.

Each employee is represented as a list [name, department, salary, active] where name is a string, department is a string, salary is an integer, and active is 1 (true) or 0 (false).

Function Signature

def filter_and_group(employees: list[list], threshold: int) -> list[list]:

Rules

  • Each record is [name, department, salary, active] . The name field does not affect the result.
  • Department names are compared exactly as given, so the comparison is case-sensitive.
  • The average for a department is the floor of its salary total divided by its number of qualifying employees.
  • Sort the pairs in ascending order of department name using plain string comparison (character code order, which is Python's default string ordering). Each department appears once, so the order is unique.
  • A department with no qualifying employee does not appear in the output. If no employee qualifies, return an empty list.

Constraints

  • 1 <= employees.length <= 10^4
  • 1 <= len(name) <= 50
  • 1 <= len(department) <= 50
  • 1 <= salary <= 10^6
  • active is 0 or 1
  • 1 <= threshold <= 10^6

Examples

Input: employees = [["Alice","Engineering",90000,1],["Bob","Engineering",80000,1],["Charlie","Marketing",70000,1],["Diana","Marketing",60000,0],["Eve","Sales",50000,1],["Frank","Sales",85000,1]], threshold = 65000
Output: [["Engineering",85000],["Marketing",70000],["Sales",85000]]

Diana is inactive and Eve's salary of 50000 is below 65000, so both are removed. Engineering averages (90000 + 80000) // 2 = 85000, while Marketing keeps only Charlie (70000) and Sales keeps only Frank (85000).

Input: employees = [["Zara","HR",55000,1],["Yuki","HR",45000,1],["Xander","HR",60000,0]], threshold = 40000
Output: [["HR",50000]]

Xander is inactive. Zara (55000) and Yuki (45000) both meet the threshold, so HR averages (55000 + 45000) // 2 = 50000.

Input: employees = [["Ann","Ops",50001,1],["Ben","Ops",50000,1],["Cy","Ads",70000,0]], threshold = 50000
Output: [["Ops",50000]]

Ben's salary equals the threshold, so he stays. The Ops total is 100001 over 2 employees, which is 50000.5 and rounds down to 50000. Ads has no active employee, so it does not appear.