Offset Commit Advancement
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 >= 00 <= len(processed) <= 200000processedmay include duplicates
Exact rules
- Offsets
<= committeddo not change the result. processedis 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)
- Build a set of processed offsets greater than
committed. - Start
cur := committed. - While
cur+1exists in set:- increment
cur
- increment
- Return
cur.
Complexity target
- Time:
O(len(processed)) - Memory:
O(len(processed))
Edge-case reminders
- Empty
processedreturns the originalcommitted. - A large offset without preceding contiguous offsets cannot advance commit.
Run tests to see results
No issues detected