KatInterview

Find Unreceived IDs in a Sequential Stream

Data Structure DesignAuto-graded · 19 test cases
MediumAuto-gradedData Structure Designsetrange-trackingamortized-analysis

Last 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 the package_id for operation i.

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^5
  • 0 <= package_id <= 10^9
  • All operation names are "report_package"
  • Duplicate package_id values 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.