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:
idKeyis an integer between1andnvalueis 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
Visualization
Time & Space Complexity
Each element is visited exactly once when it becomes part of output, so O(n) total for all operations
Array to store n values plus pointer variable
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