Assign Each Person a Tracked ID or a Fallback Value
Arrays & HashingAuto-graded · 15 test casesLast updated
Tests left-join logic and fallback lookups by requiring every person to appear with either their mapped ID or a supplied default.
You are given a roster of people, an optional table of assigned tracking numbers, and a single fallback number to use when no assignment exists.
Produce one output row per person containing their original ID, their name, and their tracking number. When a person has no entry in the assignment table, substitute the fallback value.
Input
people— a list of[person_id, name]pairs whereperson_idis a unique integer andnameis a string.assignments— a list of[person_id, tracking_id]pairs. Eachperson_idhere refers to an existing person, and no person appears more than once.fallback_id— a single integer to use for any person who has no assignment.
Output
Return a list of [person_id, name, tracking_id] lists, one per person, sorted by person_id ascending.
- Use the assigned
tracking_idwhen one exists for that person. - Use
fallback_idwhen no assignment exists for that person. - A
tracking_idof0is valid and must not be replaced by the fallback.
Constraints
0 <= len(people) <= 10^50 <= len(assignments) <= len(people)- Person names are non-empty strings and need not be unique.
person_idvalues are positive integers.fallback_idmay be any integer (negative, zero, or positive).- Each
person_idinassignmentsmatches exactly one entry inpeople. - There is at most one assignment per person.
Examples
Example 1
Input: assign_tracking_ids([[1, 'Employee A'], [2, 'Employee B']], [[2, 902]], -1)
Output: [[1, 'Employee A', -1], [2, 'Employee B', 902]]
Hints
Hint 1
Build a dictionary from the assignments list before iterating over people — this lets you look up each person's tracking ID in O(1).
Hint 2
Make sure every person appears in the result regardless of whether they have an assignment; do not discard unmatched people.
Hint 3
Use dict.get(key, fallback_id) rather than truthiness checks like `or fallback_id` — a tracking_id of 0 is legitimate and must not be overwritten.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The task is a preserve-all-left-side lookup with a default: every person must appear in the output, enriched with a tracking number when one exists and a fallback when it does not.
Step 1 — build a hash map from the assignments.
Convert the assignments list into a dictionary keyed by person_id. This costs O(m) time and space (where m = number of assignments) and makes each subsequent lookup O(1).
Step 2 — iterate over every person in sorted order.
Sort people by person_id (O(n log n)). For each person, call id_map.get(person_id, fallback_id). dict.get checks for key presence, not value truthiness, so a tracking_id of 0 is returned as-is rather than being replaced by the fallback. This mirrors a SQL LEFT JOIN followed by COALESCE.
Why it is correct.
- Every person appears exactly once because we iterate over
people, andperson_idis unique there. - The correct tracking number is chosen: the dictionary holds at most one entry per person (guaranteed by the constraints), so the lookup is unambiguous.
- Sorting by
person_id(a unique key) produces a fully determined order with no ties. - The common trap —
id_map.get(person_id) or fallback_id— treats0as falsy and wrongly substitutes the fallback. Using the two-argument form of.getavoids this entirely.
Complexity
| Cost | |
|---|---|
| Build hash map | O(m) |
| Sort people | O(n log n) |
| Lookups | O(n) |
| Total time | O(n log n + m) |
| Total space | O(n + m) |
where n = number of people and m = number of assignments.
Edge cases
- Person with no assignment → receives
fallback_id. tracking_id == 0→ kept as-is;.get(key, default)returns it correctly.- Empty
peoplelist → returns[]with no special handling needed. - All people mapped, or none mapped → both work without any branching.
from typing import List
def assign_tracking_ids(people: List[List], assignments: List[List[int]], fallback_id: int) -> List[List]:
id_map = {person_id: tracking_id for person_id, tracking_id in assignments}
return [
[person_id, name, id_map.get(person_id, fallback_id)]
for person_id, name in sorted(people, key=lambda p: p[0])
]