Offset Commit Advancement

easy · distributed-systems, streaming, logs

Offset Commit Advancement

A consumer has already committed offset committed. It has processed a set of additional offsets (unordered). It can only advance the commit to the highest contiguous offset starting from committed + 1.

Given the current committed offset and the set of processed offsets, compute the new committed offset.

Function signature

func AdvanceCommitOffset(committed int, processed []int) int

Example

committed = 4
processed = [6,5,8]
output = 6

Constraints

  • committed >= 0
  • 0 <= len(processed) <= 200000
  • processed may include duplicates

Exact rules

  • Offsets <= committed do not change the result.
  • processed is unordered and may contain duplicates.
  • Commit may advance only through a contiguous run:
    • committed+1, committed+2, ...
  • Stop at the first missing offset.

Implementation recipe (near-answer)

  1. Build a set of processed offsets greater than committed.
  2. Start cur := committed.
  3. While cur+1 exists in set:
    • increment cur
  4. Return cur.

Complexity target

  • Time: O(len(processed))
  • Memory: O(len(processed))

Edge-case reminders

  • Empty processed returns the original committed.
  • A large offset without preceding contiguous offsets cannot advance commit.
Run tests to see results
No issues detected
    Join Discord