Reassemble an Out-of-Order Byte Stream From Overlapping Segments
Last updated: November 29, 2026
Quick Overview
Implement the receive side of a TCP-like stream: segments arrive with a starting byte offset and payload, possibly out of order, duplicated, or overlapping; deliver contiguous bytes to the application as soon as they are available while bounding buffered memory.
0
0
0 solved
Implement the receive side of a TCP-like stream: segments arrive with a starting byte offset and payload, possibly out of order, duplicated, or overlapping; deliver contiguous bytes to the application as soon as they are available while bounding buffered memory.
Teams working on QUIC in Chrome and Google's front ends implement exactly this stream reassembly. The interviewer checks interval handling, correct trimming of overlaps, and awareness that a hostile peer can exhaust memory with gaps.
What the Interviewer Expects
- Track the next expected offset
- Trim data before the expected offset and discard full duplicates
- Buffer future segments keyed by offset
- Deliver contiguous runs as soon as the gap closes
- Enforce a receive window to bound memory
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- How would you handle segments that disagree on bytes in the overlap?
- How do SACK blocks get derived from this buffer?
- How would you reduce copying for large payloads?
- What changes for QUIC streams with a final size?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
Reassembler(window).receive(offset, data)returns bytes newly deliverable to the app, in order.- Bytes before the delivered point are ignored; bytes beyond
delivered + windoware dropped (...
High-Level Design
State: delivered (next offset owed to the app) and pending, a dict offset -> bytes of non-overlapping out-of-order chunks.
On receive: clip the segment to [delivered, delivered + window). The...