Find Unreceived IDs in a Sequential Stream
Data Structure DesignAuto-graded · 19 test casesLast updated
Tests efficient gap tracking in a sequential ID space using sets and amortized range updates.
You are building a monitoring system that receives sequential delivery confirmations. Each delivery is assigned a unique nonnegative integer ID starting from 0. Confirmations arrive out of order.
Write a function that processes a stream of confirmation operations. Each operation is "report_package" with one argument package_id (a nonnegative integer). After processing each confirmation, return a sorted list of all IDs that are currently unreceived but fall below the highest ID seen so far.
Formally, if the maximum ID received up to this point is H, return all integers i where 0 <= i < H that have not yet been confirmed, sorted in ascending order.
Input Format
operations: A list of operation names. Every entry will be"report_package".arguments: A list of argument lists.arguments[i][0]is thepackage_idfor operationi.
Both lists have the same length, representing the sequence of calls to process.
Output Format
Return a list of lists. The i-th inner list is the sorted list of missing IDs after processing operation i.
Constraints
1 <= len(operations) <= 10^50 <= package_id <= 10^9- All operation names are
"report_package" - Duplicate
package_idvalues may appear
Examples
Example 1
Input: find_unreceived_ids(['report_package', 'report_package', 'report_package'], [[2], [0], [1]])
Output: [[0, 1], [1], []]
Example 2
Input: find_unreceived_ids(['report_package', 'report_package', 'report_package', 'report_package'], [[5], [3], [4], [0]])
Output: [[0, 1, 2, 3, 4], [0, 1, 2, 4], [0, 1, 2], [1, 2]]
Hints
Hint 1
Instead of tracking which IDs have been received, track which IDs are currently missing below the maximum.
Hint 2
When a new maximum arrives, every ID strictly between the old maximum and the new one becomes missing.
Hint 3
Use a set to store missing IDs and update it incrementally; each ID enters the set at most once.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Rather than storing every ID that has been received and then scanning the entire range [0, highest] to find gaps, we maintain a set of IDs that are currently missing. This set changes in only two ways:
- A new maximum ID arrives: All IDs strictly between the previous maximum and the new one have never been seen, so they become missing.
- Any ID is confirmed: Remove it from the missing set if present (no-op for duplicates or IDs at or above the maximum).
We track highest, initialized to -1 so that the first confirmation correctly identifies all IDs from 0 to first_id - 1 as missing.
For each operation:
- If
package_id > highest, add every integer inrange(highest + 1, package_id)to the missing set, then updatehighest = package_id. - Remove
package_idfrom the missing set (does nothing if already absent). - Append the sorted missing IDs to the results.
Why It Works
The invariant maintained after each operation is:
missingcontains exactly those IDsiwhere0 <= i < highestandihas not been confirmed.
When package_id > highest:
IDs in [0, old_highest) retain their status. IDs in (old_highest, package_id) have never appeared in any prior operation (since all prior IDs were <= old_highest), so they all become missing. The range (old_highest, package_id) is represented as range(highest + 1, package_id), which excludes both endpoints. We then discard package_id from the set as a safeguard, though it was never added.
When package_id <= highest:
No new maximum, so no new gaps appear. If package_id was missing, it is now confirmed and removed. If it's a duplicate or equal to highest, the discard has no effect.
Both cases preserve the invariant, so the returned list is always correct.
Complexity
Let N = number of operations and M = highest ID + 1 (the span of the ID range).
- Time: Each integer in
[0, highest)is added tomissingat most once across all operations, since ranges never overlap. Gap insertion totals O(M) amortized. Each discard is O(1). Sorting the missing set on each call costs O(k log k) where k = |missing|. Overall: O(N + M + Σ kᵢ log kᵢ) across all operations. - Space: O(M) for the missing set in the worst case, plus O(output size) for the results.
In practice, when confirmations are mostly in order, the missing set stays small and performance is excellent.
Implementation
from typing import List
def find_unreceived_ids(operations: List[str], arguments: List[List[int]]) -> List[List[int]]:
missing = set()
highest = -1
results = []
for op, args in zip(operations, arguments):
if op != "report_package":
raise ValueError("unknown operation")
pkg_id = args[0]
if pkg_id > highest:
missing.update(range(highest + 1, pkg_id))
highest = pkg_id
missing.discard(pkg_id)
results.append(sorted(missing))
return results