The Design an Ordered Stream interview question asks you to build a system that receives packets of data (ID and value) out of order but must output them in a specific sequence. You maintain a pointer starting at 1. Every time a new packet arrives, you store it. If the packet's ID matches the current pointer, you return that value along with any subsequent values that have already arrived and are now in order.
Companies like Meta and Google ask the Design an Ordered Stream coding problem to evaluate your understanding of Data Stream interview patterns. It mimics real-world scenarios like TCP packet reassembly or video streaming, where segments arrive sporadically but must be processed chronologically. It tests your ability to manage state and pointers within an array.
This problem follows the Pointer and Array/Map Simulation pattern.
ptr variable initialized to 1.insert method:
stream[id].id == ptr, start a loop to collect all consecutive non-null values starting from ptr.ptr to the next missing ID and return the collected list.Stream size: 5.
insert(3, "ccccc"): Pointer is at 1. stream[3] is now "ccccc". Return [].insert(1, "aaaaa"): Pointer is at 1. id == ptr.
stream[1] ("aaaaa").stream[2] is empty. Stop.["aaaaa"].insert(2, "bbbbb"): Pointer is at 2. id == ptr.
stream[2] ("bbbbb").stream[3] is "ccccc".stream[4] is empty. Stop.["bbbbb", "ccccc"].Think of the pointer as a "frontier." Data behind the pointer is already emitted; data at the pointer is the next bottleneck. This mental model helps in all sorts of "Reordering Buffer" problems common in systems engineering.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Two Sum III - Data structure design | Easy | Solve | |
| Detect Squares | Medium | Solve | |
| Logger Rate Limiter | Easy | Solve | |
| First Unique Number | Medium | Solve | |
| Finding Pairs With a Certain Sum | Medium | Solve |