AlgoMaster Logo
AlgoMasterAdvance a Safe Outbox Checkpointmedium

Advance a Safe Outbox Checkpoint

medium

An outbox relay may publish several events concurrently, so acknowledgements can arrive out of order. Its durable checkpoint must represent the largest sequence for which every earlier sequence is also confirmed; otherwise a restart could skip an unacknowledged event.

Design an OutboxCheckpointTracker class:

  • OutboxCheckpointTracker(int initialCheckpoint) initializes the last contiguous confirmed sequence.
  • int acknowledge(int sequence) records one acknowledgement and returns the resulting checkpoint.
  • int checkpoint() returns the current checkpoint.

An acknowledgement at or below the checkpoint is a duplicate and has no effect. An acknowledgement above the checkpoint must be remembered. After recording it, repeatedly advance while checkpoint + 1 has been acknowledged.

The checkpoint never moves backward and never crosses a missing sequence.

Example 1:

Input:

Output:

Explanation: Sequence 2 cannot advance past missing sequence 1. Once 1 is acknowledged, both consecutive values are confirmed.

Example 2:

Input:

Output:

Explanation: Acknowledgement 8 is retained. When 7 arrives, the checkpoint advances across both 7 and the already stored 8.

Constraints

  • -1 <= initialCheckpoint <= 10^9
  • 0 <= sequence <= 10^9
  • At most 1000 method calls are made per object.
  • Acknowledgements may be duplicated and arrive in any order.
Hints

Loading...
CallReturns
new OutboxCheckpointTracker(0)null
acknowledge(2)0
acknowledge(1)2
checkpoint()2

Acknowledging 2 first leaves a gap at 1. Once 1 arrives, the checkpoint advances through both confirmed sequences.

Run checks these cases. Submit also runs a larger hidden set.