AlgoMaster Logo
AlgoMasterCommit Contiguous Consumer Offsetsmedium

Commit Contiguous Consumer Offsets

medium

Consumers can process records from one partition concurrently, so acknowledgments may arrive out of order. Committing past a gap is unsafe: after a restart, the missing record would appear complete and might never be delivered again.

Design a PartitionOffsetTracker class:

  • PartitionOffsetTracker(int partitions) creates partitions numbered 0 through partitions - 1. Every committed offset starts at -1.
  • int ack(int partition, int offset) records a successful acknowledgment and returns that partition's committed offset.

The committed offset is the greatest c for which every offset from 0 through c has been acknowledged. Acknowledgments may arrive in any order and may be repeated. State is independent across partitions, and committed offsets never decrease.

Example 1:

Input:

Output:

Explanation: Offset 1 waits for 0. Later, offset 3 waits for 2. Partition 1 advances independently.

Example 2:

Input:

Output:

Explanation: The repeated acknowledgment of offset 0 is harmless.

Constraints

  • 1 <= partitions <= 100
  • 0 <= partition < partitions
  • 0 <= offset <= 10^9
  • At most 10^5 calls are made to ack.
Hints

Loading...
CallReturns
new PartitionOffsetTracker(2)null
ack(0, 1)-1
ack(0, 0)1
ack(1, 0)0
ack(0, 3)1
ack(0, 2)3

Partition 0 cannot commit offset 1 until offset 0 arrives. Offset 3 also waits until offset 2 closes the next gap. Partition 1 advances independently.

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