Sign in to run and submit your work
Reading is open to everyone. Running code and saving drafts need an account so your work is yours and comes back on your next visit.
or
CODE WORKSPACE
The consumer records the offset of every message it processes. A gap in those offsets means messages were lost — a genuine incident — but the current audit reports a gap of nine hundred whenever a producer restarts and its offsets return to zero, so the report is ignored.
Write detect_sequence_gaps(records, partition_field, offset_field). Return one report per partition.
Function to write
detect_sequence_gaps(records: list[dict], partition_field: str, offset_field: str) -> list[dict]One report per partition with the first and last offsets, the count, and the gaps, duplicates and resets.
How to approach it
Keep a high-water mark and a set of what you have seen, and never let the mark go down.
Sample cases
+ 2 held back until you submit
four partitions with different faults
A genuine gap, a duplicate delivery, a producer restart back to zero, and an unusable offset.
Input
Argument 1
| partition | offset |
|---|---|
| 0 | 100 |
| 0 | 101 |
| 0 | 105 |
| 0 | 106 |
| 1 | 50 |
| 1 | 51 |
| 1 | 51 |
| 1 | 52 |
| 2 | 900 |
| 2 | 901 |
| 2 | 0 |
| 2 | 1 |
| 3 | -5 |
Argument 2
'partition'Argument 3
'offset'Returns
[
{
'partition': 0,
'first': 100,
'last': 106,
'count': 4,
'gaps': [
{
'from': 102,
'to': 104,
'missing': 3
}
],
'duplicates': [],
'resets': []
},
{
'partition': 1,
'first': 50,
'last': 52,
'count': 4,
'gaps': [],
'duplicates': [
51
],
'resets': []
},
{
'partition': 2,
'first': 0,
'last': 901,
'count': 4,
'gaps': [],
'duplicates': [],
'resets': [
0,
1
]
}
]nothing consumed
An empty consumption window reports nothing rather than a partition with no offsets.
Input
Argument 1
[] (empty list)
Argument 2
'partition'Argument 3
'offset'Returns
[] (empty list)
a clean stream
A contiguous run reports no gaps, no duplicates and no resets.
Input
Argument 1
| partition | offset |
|---|---|
| 0 | 1 |
| 0 | 2 |
| 0 | 3 |
Argument 2
'partition'Argument 3
'offset'Returns
[
{
'partition': 0,
'first': 1,
'last': 3,
'count': 3,
'gaps': [],
'duplicates': [],
'resets': []
}
]Constraints
Worked example
Partition 0 goes 100, 101, then 105. Offsets 102, 103 and 104 were never consumed — that is a real gap of 3 messages and the thing the audit exists to find. Partition 2 goes 900, 901, then 0, then 1. Comparing consecutive offsets reports a gap of nine hundred, which is what makes the report unreadable. It is a producer restart: the offsets went backwards, no messages are missing, and the high-water mark must stay at 901 so the 0 and the 1 do not each open a gap of their own. Partition 1 receives 51 twice. At-least-once delivery guarantees this happens, so it is normal rather than alarming — but it is worth counting, because a consumer whose duplicate rate suddenly rises is usually one that is failing and retrying.
What this tests
Reading a sequence for three different faults at once, and knowing that 'inconsistent with the previous value' is not one fault but several. Sorting the input first is the instinct that makes two of the three undetectable.
detect_sequence_gaps(records: list[dict], partition_field: str, offset_field: str) -> list[dict]Submit for review to find out what your query gets right, what it gets wrong, and how it compares with the best working query for this exercise.
This scenario runs a full workspace — editor, canvas and results side by side. It needs a laptop or desktop to be usable. Open this page on a bigger screen to start building.