Skip to main content

Stream Message Committer

easy
Software engineer

In a multi-threaded system, messages are identified by unique, non-negative offsets starting from 0. While messages are processed out of order, the system can only commit an offset if all messages with smaller offsets have already been processed.

Commit Rules

  • Commit Definition: Committing an offset x implies that all messages from 0 to x inclusive are now acknowledged.
  • Eligibility: Offset 0 is always eligible once processed. Any offset x > 0 is eligible only if every offset from 0 to x-1 has already been processed or committed.
  • Output: For each message in the input offsets array (representing the order in which they arrive/finish processing):
    • If the current message allows for a new commit, output the highest offset that can now be committed.
    • If the contiguous block starting from the last committed offset (or 0) is still incomplete, output -1.

Example 1

Input

offsets = [2, 0, 1]

Output

[-1, 0, 2]

Explanation

1. Offset 2 is processed. We cannot commit because 0 and 1 are missing. Output -1.
2. Offset 0 is processed. 0 is the starting point, so we commit 0. Output 0.
3. Offset 1 is processed. Now we have 0, 1, and 2. The highest contiguous offset is 2. Output 2.

Example 2

Input

offsets = [0, 1, 2]

Output

[0, 1, 2]

Explanation

Each message arrives in order, allowing the commit pointer to move forward immediately at each step.

Example 3

Input

offsets = [2, 1, 0, 5, 4]

Output

[-1, -1, 2, -1, -1]

Explanation

We only see a commit when offset 0 arrives (completing the block 0-2). When 5 and 4 arrive, they are still waiting on offset 3 to complete the next block.

Constraints

  • 1 <= offsets.length <= 10⁵
  • 0 <= offsets[i] < offsets.length
  • All values in offsets are unique.

Screening

ArrayHash TableSimulationTwo Pointers
Language
Code editor loads in the browser.

Output

Input

[2,0,1]

Expected

[-1,0,2]

Your output

Run to see your output.