Roster Screening with Optional Age, Pay, and Job Limits
Arrays & HashingAuto-graded · 19 test casesLast 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: stringlast_name: stringage: integeroccupation: stringsalary: integer
Parameters
The function receives, in this order:
roster: the list of records described above.min_age: integer orNone.max_age: integer orNone.jobs: a list of accepted occupation strings, orNone.min_salary: integer orNone.max_salary: integer orNone.
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_ageenabled: the record'sagemust be greater than or equal tomin_age.max_ageenabled: the record'sagemust be less than or equal tomax_age.jobsenabled: the record'soccupationmust be one of the strings injobs.min_salaryenabled: the record'ssalarymust be greater than or equal tomin_salary.max_salaryenabled: the record'ssalarymust be less than or equal tomax_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
jobsis 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.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Each record must pass a conjunction of up to five independent checks, and each check applies only when its parameter is not None. A single pass over the roster is enough:
- Convert
jobsto a set once (or keepNoneif the rule is disabled). - For every record, in order, test each enabled rule. If any test fails, skip the record.
- Otherwise append the record to the result list.
Why it is correct
- Inclusive bounds: a record is rejected only when it strictly violates a bound (
age < min_age,age > max_age, and the same for salary), so values exactly on a bound are kept. - Disabled rules: every check is guarded by
is not None, so aNoneparameter disables only its own check. Usingis not Nonerather than truthiness ensures that0is treated as a real bound. - Order: records are appended in the order they are visited, so the output is a subsequence of the input.
- No mutation: the function only reads fields and builds a new list. The input list and the records are never changed, and a new list is returned even when no rules are enabled.
- Case sensitivity: Python string equality and set membership are exact, so no normalization is applied.
Complexity
- Time: O(n + k), where n is the number of records and k is the number of accepted jobs. Building the set costs O(k); each record needs at most five constant-time checks.
- Space: O(k) for the set plus O(m) for the output list of m references.
Reference solution
from typing import Dict, List, Optional
def screen_roster(
roster: List[Dict[str, object]],
min_age: Optional[int],
max_age: Optional[int],
jobs: Optional[List[str]],
min_salary: Optional[int],
max_salary: Optional[int],
) -> List[Dict[str, object]]:
accepted = set(jobs) if jobs is not None else None
result = []
for rec in roster:
age = rec["age"]
salary = rec["salary"]
if min_age is not None and age < min_age:
continue
if max_age is not None and age > max_age:
continue
if accepted is not None and rec["occupation"] not in accepted:
continue
if min_salary is not None and salary < min_salary:
continue
if max_salary is not None and salary > max_salary:
continue
result.append(rec)
return result