KatInterview

Roster Screening with Optional Age, Pay, and Job Limits

Arrays & HashingAuto-graded · 19 test cases
EasyAuto-gradedArrays & Hashingfilteringhash-setoptional-parameters

Last updated

Scan a list of employee records and keep those meeting every enabled optional range or membership rule, preserving order and leaving the input untouched.

Roster Screening with Optional Age, Pay, and Job Limits

You are given a roster of employee records and up to five optional screening rules. Write a function that returns the records passing every rule that is switched on.

Each record in roster is a dictionary with these keys:

  • first_name: string
  • last_name: string
  • age: integer
  • occupation: string
  • salary: integer

Parameters

The function receives, in this order:

  1. roster: the list of records described above.
  2. min_age: integer or None.
  3. max_age: integer or None.
  4. jobs: a list of accepted occupation strings, or None.
  5. min_salary: integer or None.
  6. max_salary: integer or None.

Rules

A rule is enabled when its parameter is not None, and disabled when it is None. A disabled rule has no effect on any other rule. A record is kept only if it satisfies every enabled rule:

  • min_age enabled: the record's age must be greater than or equal to min_age.
  • max_age enabled: the record's age must be less than or equal to max_age.
  • jobs enabled: the record's occupation must be one of the strings in jobs.
  • min_salary enabled: the record's salary must be greater than or equal to min_salary.
  • max_salary enabled: the record's salary must be less than or equal to max_salary.

Note that a bound of 0 is a real, enabled bound. Only None disables a rule.

Output

Return a new list of the qualifying records, in the same relative order they had in roster. The records themselves may be the same dictionary objects as in the input. If no rules are enabled, return a new list holding all the records. If nothing qualifies (or the roster is empty), return an empty list.

Do not modify the input list or any of its records.

Constraints

  • 0 <= len(roster) <= 200,000
  • Ages and salaries fit in signed 64-bit integers.
  • When both bounds of a pair are given, the lower bound is not greater than the upper bound.
  • When jobs is given, it is nonempty.
  • String comparisons are case-sensitive and exact ("Engineer" differs from "engineer").

Examples

Example 1

Input: screen_roster([{'first_name': 'Ada', 'last_name': 'Ng', 'age': 27, 'occupation': 'engineer', 'salary': 120000}, {'first_name': 'Sam', 'last_name': 'Lee', 'age': 35, 'occupation': 'designer', 'salary': 95000}, {'first_name': 'Mina', 'last_name': 'Park', 'age': 42, 'occupation': 'engineer', 'salary': 150000}], 30, 45, ['engineer', 'designer'], 100000, None)

Output: [{'first_name': 'Mina', 'last_name': 'Park', 'age': 42, 'occupation': 'engineer', 'salary': 150000}]

Example 2

Input: screen_roster([], None, None, None, None, None)

Output: []

Example 3

Input: screen_roster([{'first_name': 'Jo', 'last_name': 'Kim', 'age': 30, 'occupation': 'Engineer', 'salary': 100000}], 30, 30, ['Engineer'], 100000, 100000)

Output: [{'first_name': 'Jo', 'last_name': 'Kim', 'age': 30, 'occupation': 'Engineer', 'salary': 100000}]

Hints

Hint 1

Treat each of the five rules independently: a rule only matters if its parameter is not None. Be careful to test with `is not None` rather than truthiness, since 0 is a valid bound.

Hint 2

Checking membership in a list for every record is slow when the list of jobs is long. Convert the accepted jobs to a set once, before the loop.

Hint 3

Walk through the roster once, in order, appending qualifying records to a fresh list. That gives stable ordering and avoids mutating the input.