Design an Ordered Stream - Problem

There is a stream of n pairs (idKey, value) arriving in an arbitrary order, where idKey is an integer between 1 and n and value is a string. No two pairs have the same id.

Design a stream that returns the values in increasing order of their IDs by returning a chunk (list) of values after each insertion. The concatenation of all the chunks should result in a list of the sorted values.

Implement the OrderedStream class:

  • OrderedStream(int n) Constructs the stream to take n values.
  • String[] insert(int idKey, String value) Inserts the pair (idKey, value) into the stream, then returns the largest possible chunk of currently inserted values that appear next in the order.

Input & Output

Example 1 — Basic Stream Operations
$ Input: n = 5, operations = [[3,"ccccc"],[1,"aaaaa"],[2,"bbbbb"],[5,"eeeee"],[4,"ddddd"]]
Output: [[],["aaaaa"],["aaaaa","bbbbb","ccccc"],[],["ddddd","eeeee"]]
💡 Note: Insert (3,"ccccc"): ptr=1, arr[1] is empty, return []. Insert (1,"aaaaa"): ptr=1, arr[1]="aaaaa", return ["aaaaa"], ptr=2. Insert (2,"bbbbb"): ptr=2, found consecutive values at positions 2,3, return ["bbbbb","ccccc"], ptr=4.
Example 2 — Sequential Order
$ Input: n = 3, operations = [[1,"first"],[2,"second"],[3,"third"]]
Output: [["first"],["second"],["third"]]
💡 Note: Each insert returns exactly one value since they arrive in order: ptr advances one position each time.
Example 3 — Reverse Order
$ Input: n = 3, operations = [[3,"third"],[2,"second"],[1,"first"]]
Output: [[],[],["first","second","third"]]
💡 Note: First two inserts return [] since ptr=1 is not filled. When (1,"first") arrives, all consecutive values 1,2,3 are returned.

Constraints

  • 1 ≤ n ≤ 1000
  • 1 ≤ idKey ≤ n
  • value.length == 5
  • value consists only of lowercase letters
  • Each call to insert will have a unique idKey

Visualization

Tap to expand
Ordered Stream - Array-Based Direct Access INPUT Stream Array (n=5) idx 1 idx 2 idx 3 idx 4 idx 5 Operations: 1. insert(3, "ccccc") 2. insert(1, "aaaaa") 3. insert(2, "bbbbb") 4. insert(5, "eeeee") 5. insert(4, "ddddd") ptr: tracks next ID to return Initial: ptr = 1 ALGORITHM STEPS 1 Insert at idKey Store value at stream[idKey] 2 Check Pointer If stream[ptr] exists, collect 3 Advance Pointer Move ptr++ while values exist 4 Return Chunk Return collected values as list Execution Trace: Op Array State ptr Output (3,c) [_,_,c,_,_] 1 [] (1,a) [a,_,c,_,_] 2 [a] (2,b) [a,b,c,_,_] 4 [a,b,c] (5,e) [a,b,c,_,e] 4 [] (4,d) [a,b,c,d,e] 6 [d,e] FINAL RESULT Final Stream Array: aaaaa [1] bbbbb [2] ccccc [3] ddddd [4] eeeee [5] Output Chunks: Op 1: [] Op 2: ["aaaaa"] Op 3: ["aaaaa","bbbbb","ccccc"] Op 4: [] Op 5: ["ddddd","eeeee"] Concatenated Result: ["aaaaa","bbbbb","ccccc", "ddddd","eeeee"] OK - Sorted! Key Insight: Array-based direct access uses O(1) time for each insert. The pointer tracks the next expected ID. When a value arrives at the pointer position, we advance and collect all consecutive values until a gap. Time: O(n) total | Space: O(n) for the stream array TutorialsPoint - Design an Ordered Stream | Array-Based Direct Access
Asked in
Google 25 Amazon 18
28.5K Views
Medium Frequency
~15 min Avg. Time
856 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