Skip to content

Practice Design an Ordered Stream

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#1656
LeetCode Problem

Design an Ordered Stream

Problem Statement

OrderedStream(n) receives unique IDs from 1 through n out of order. insert(idKey,value) stores a pair and returns the largest consecutive chunk that can now be emitted in increasing ID order, starting after the last emitted value. Missing next IDs produce an empty chunk. Practice approach: Keep an array indexed by ID and a pointer starting at one. After insertion, advance and emit while the pointer identifies a stored value. Each entry is emitted once, giving O(n) total emission work and O(n) storage. Do not re-emit earlier chunks.

Source: Design an Ordered Stream on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
["OrderedStream","insert","insert","insert"]
[[3],[2,"bravo"],[1,"alpha"],[3,"cider"]]
Output
[null,[],["alpha","bravo"],["cider"]]
Explanation

The first pair waits for ID 1; receiving ID 1 releases both pending entries.

Constraints

1 <= n <= 1000
1 <= idKey <= n
Values have five lowercase letters.
Exactly n insert calls use unique IDs.

Related LeetCode Problems

Practice more problems similar to Design an Ordered Stream.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.