Lists & Stacks Hard
Segmented List Iterator
A segmented list stores an ordered list of lists while exposing one global sequence. Its iterator tracks both the current segment and the element within that segment.
Advanced input, scenarios & complexity
Trace input
Bounded JSONEdit the exact operation sequence, then build a deterministic trace.
Source scenarios
Complexity
BestO(1) for a global get in the first segment
AverageO(S + local get)
WorstO(S + local get)
SpaceO(N + S)
Data structure in memory
Follow the active references as the source executes.
Operation result
Current stepReady to explore
Source behavior & common mistakes
Add or remove whole segments, read a global index across segment boundaries, and step a fail-fast iterator across empty and nonempty lists.
- Flattening segments loses the segment boundaries and iterator cursor behavior.
- An empty segment is skipped by hasNext, which advances both iterator cursors.
- removeSegment increments nOp before checking its index, so even a failed removal invalidates iterators.
- The original lists are retained by reference; external list edits are visible but do not increment nOp.
- A null list reference is stored by addSegment and fails only when later dereferenced.
Algorithm code
JavaPinned upstream — SegmentedList.javaStep linked
The highlighted line follows the current step.
Ready to begin
Press Play to see what happens.
Detailed execution
Snapshot 1 / 1
Comparisons 0 · Writes 0
Understand this operation
Scroll through the stored teaching scenes. The Theater keeps your place.