KatInterview

Assign Each Person a Tracked ID or a Fallback Value

Arrays & HashingAuto-graded · 15 test cases
EasyAuto-gradedArrays & Hashinghash-maplookupleft-join

Last 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 where person_id is a unique integer and name is a string.
  • assignments — a list of [person_id, tracking_id] pairs. Each person_id here 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_id when one exists for that person.
  • Use fallback_id when no assignment exists for that person.
  • A tracking_id of 0 is valid and must not be replaced by the fallback.

Constraints

  • 0 <= len(people) <= 10^5
  • 0 <= len(assignments) <= len(people)
  • Person names are non-empty strings and need not be unique.
  • person_id values are positive integers.
  • fallback_id may be any integer (negative, zero, or positive).
  • Each person_id in assignments matches exactly one entry in people.
  • 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.