Design an Ordered Stream - Problem

Design an Ordered Stream - Imagine you're building a streaming data processor that receives key-value pairs out of order but needs to output them in sorted order!

You have a stream of n pairs (idKey, value) arriving in arbitrary order, where:

  • idKey is an integer between 1 and n
  • value is a string
  • No two pairs have the same idKey

Your task is to design a stream that returns values in increasing order of their IDs by returning a chunk (list) of values after each insertion. The key insight is that you should return the largest possible chunk of consecutive values that can be output in order.

Example: If you have slots [1,2,3,4,5] and receive pairs in order: (3,"c"), (1,"a"), (2,"b"), you should output: [], ["a"], ["b","c"] respectively.

Input & Output

example_1.py โ€” Basic Stream
$ Input: OrderedStream os = new OrderedStream(5); os.insert(3, "ccccc"); // Returns [] os.insert(1, "aaaaa"); // Returns ["aaaaa"] os.insert(2, "bbbbb"); // Returns ["bbbbb", "ccccc"] os.insert(5, "eeeee"); // Returns [] os.insert(4, "ddddd"); // Returns ["ddddd", "eeeee"]
โ€บ Output: [[], ["aaaaa"], ["bbbbb", "ccccc"], [], ["ddddd", "eeeee"]]
๐Ÿ’ก Note: Initially all positions empty. Insert (3,'ccccc') but can't output until we have position 1. Insert (1,'aaaaa') outputs ['aaaaa'] and advances pointer to 2. Insert (2,'bbbbb') finds consecutive 'bbbbb' and 'ccccc' at positions 2,3.
example_2.py โ€” Sequential Insert
$ Input: OrderedStream os = new OrderedStream(3); os.insert(1, "aaa"); // Returns ["aaa"] os.insert(2, "bbb"); // Returns ["bbb"] os.insert(3, "ccc"); // Returns ["ccc"]
โ€บ Output: [["aaa"], ["bbb"], ["ccc"]]
๐Ÿ’ก Note: When elements arrive in order, each insertion returns a single-element array with that element, as the pointer can advance immediately each time.
example_3.py โ€” Reverse Order
$ Input: OrderedStream os = new OrderedStream(3); os.insert(3, "ccc"); // Returns [] os.insert(2, "bbb"); // Returns [] os.insert(1, "aaa"); // Returns ["aaa", "bbb", "ccc"]
โ€บ Output: [[], [], ["aaa", "bbb", "ccc"]]
๐Ÿ’ก Note: Elements arrive in reverse order. First two insertions return empty arrays because position 1 is not filled. Final insertion triggers output of all three consecutive elements.

Visualization

Tap to expand
Restaurant Order Window AnalogyKitchen(Completes out of order)Service Window(Holds completed orders)Waiter(Serves in order)Step 1: Order #3 completed first#3โ†’ Waiter waiting for #1#1?#2?#3Can't serve yet!Step 2: Order #1 completed#1โ†’ Can serve #1 now!#1#2?#3Serve: [#1]Step 3: Order #2 completed#2โ†’ Can serve #2 and #3!done#2#3Serve: [#2, #3]๐ŸŽฏ Key Insight: Track next expected order with a pointer!
Understanding the Visualization
1
Order #3 Ready
Kitchen completes order #3 first, but waiter can't serve it until orders #1 and #2 are ready
2
Order #1 Ready
Kitchen completes order #1, waiter can now serve it immediately
3
Order #2 Ready
Kitchen completes order #2, waiter can serve both #2 and the waiting #3 consecutively
Key Takeaway
๐ŸŽฏ Key Insight: Like a restaurant waiter tracking the next order to serve, we use a pointer to track the next expected position, avoiding unnecessary rescans of already-processed elements.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Each element is visited exactly once when it becomes part of output, so O(n) total for all operations

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Array to store n values plus pointer variable

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค n โ‰ค 1000
  • 1 โ‰ค idKey โ‰ค n
  • idKey is unique for each call
  • 1 โ‰ค value.length โ‰ค 5
  • value consists only of lowercase letters
  • At most n calls will be made to insert
Asked in
Google 12 Amazon 8 Apple 6 Microsoft 4
24.3K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen