KatInterview

Earliest Non-Repeating ID in a Live Feed Replay

Data Structure DesignAuto-graded · 22 test cases
MediumAuto-gradedData Structure Designhash-mapqueuedesign

Last updated

Replay a sequence of operations on a stream of integer IDs and report, at each query, the earliest-arriving ID seen exactly once, using a hash map plus an ordered queue.

Earliest Non-Repeating ID in a Live Feed Replay

A service receives a feed of positive integer IDs, one at a time. At any moment you may be asked for the earliest non-repeating ID: among all IDs that have appeared exactly once so far, the one whose arrival came first. If no ID has appeared exactly once, the answer is -1.

Rather than building a class, you implement one function that replays a recorded list of operations and returns the outputs produced by each of them.

Operations

Each operation is a name paired with an argument list at the same position:

Operation nameArgumentsEffectOutput
"RestaurantTracker"[]Resets the tracker to a fresh, empty state, discarding everything seen earlier.None
"add_restaurant"[id]Records the arrival of id at the end of the feed.None
"get_first_unique"[]Reports the earliest-arriving ID that has appeared exactly once so far, or -1 if there is none.that ID or -1

Any other operation name must raise a ValueError.

Task

Given operations and arguments (parallel lists of equal length), process the operations from left to right and return a list with exactly one output per operation, in the same order.

Rules

  • "Earliest" refers to the position of an ID's first (and only) arrival in the feed.
  • An ID that has appeared two or more times is never considered unique, even if later arrivals are of other IDs.
  • A query on an empty feed returns -1.
  • A query does not change the feed; repeating it without new arrivals yields the same answer.
  • After a reset operation, IDs from before the reset have no influence on later answers.

Constraints

  • 1 <= id <= 10^6
  • The total number of add_restaurant operations does not exceed 10^5.
  • len(operations) == len(arguments)
  • Aim for amortized O(1) time per operation.

Examples

Example 1

Input: first_unique_replay(['RestaurantTracker', 'add_restaurant', 'add_restaurant', 'add_restaurant', 'get_first_unique', 'add_restaurant', 'get_first_unique'], [[], [101], [202], [101], [], [303], []])

Output: [None, None, None, None, 202, None, 202]

Example 2

Input: first_unique_replay(['RestaurantTracker', 'get_first_unique'], [[], []])

Output: [None, -1]

Example 3

Input: first_unique_replay(['RestaurantTracker', 'add_restaurant', 'add_restaurant', 'add_restaurant', 'get_first_unique', 'add_restaurant', 'get_first_unique', 'add_restaurant', 'get_first_unique'], [[], [7], [7], [8], [], [8], [], [9], []])

Output: [None, None, None, None, 8, None, -1, None, 9]

Hints

Hint 1

Keeping only a frequency map makes each query require a scan over all IDs in arrival order. You also need to remember arrival order somewhere.

Hint 2

Put an ID into a queue only at the moment its count becomes 1. Later duplicates do not need to touch the queue at all.

Hint 3

When answering a query, discard entries from the front of the queue whose count is no longer 1. Counts never decrease, so a discarded ID can never become unique again, and each ID is pushed and popped at most once.