Design an Ordered Stream
EasyPrompt
There is a stream of n (idKey, value) pairs 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 takenvalues.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.
Example:

Input
["OrderedStream", "insert", "insert", "insert", "insert", "insert"]
[[5], [3, "ccccc"], [1, "aaaaa"], [2, "bbbbb"], [5, "eeeee"], [4, "ddddd"]]
Output
[null, [], ["aaaaa"], ["bbbbb", "ccccc"], [], ["ddddd", "eeeee"]]
Explanation
// Note that the values ordered by ID is ["aaaaa", "bbbbb", "ccccc", "ddddd", "eeeee"].
OrderedStream os = new OrderedStream(5);
os.insert(3, "ccccc"); // Inserts (3, "ccccc"), returns [].
os.insert(1, "aaaaa"); // Inserts (1, "aaaaa"), returns ["aaaaa"].
os.insert(2, "bbbbb"); // Inserts (2, "bbbbb"), returns ["bbbbb", "ccccc"].
os.insert(5, "eeeee"); // Inserts (5, "eeeee"), returns [].
os.insert(4, "ddddd"); // Inserts (4, "ddddd"), returns ["ddddd", "eeeee"].
// Concatentating all the chunks returned:
// [] + ["aaaaa"] + ["bbbbb", "ccccc"] + [] + ["ddddd", "eeeee"] = ["aaaaa", "bbbbb", "ccccc", "ddddd", "eeeee"]
// The resulting order is the same as the order above.
Constraints:
1 <= n <= 10001 <= id <= nvalue.length == 5valueconsists only of lowercase letters.- Each call to
insertwill have a uniqueid. - Exactly
ncalls will be made toinsert.
Approaches
2 approaches with complexity analysis and trade-offs.
This approach uses a HashMap to store the incoming (idKey, value) pairs in an unordered fashion. A separate pointer, initialized to 1, keeps track of the next expected ID. When a new pair is inserted, it's added to the map. Then, we check if the map contains the value for the current pointer. If it does, we start collecting a chunk of contiguous values by incrementing the pointer until we find a missing ID. This method is straightforward but has slightly more overhead than using an array.
Algorithm
- In the constructor, initialize a
HashMap<Integer, String>and an integerptrto1. - In the
insert(idKey, value)method:- Store the
(idKey, value)pair in the HashMap. - Create an empty list
chunkto store the result. - Start a
whileloop that continues as long as the HashMap contains the keyptr. - Inside the loop, add the value for
ptrto thechunkand incrementptr. - After the loop, return the
chunk.
- Store the
Walkthrough
This approach maintains the state of the stream using a HashMap and an integer pointer.
-
Data Structures:
map: AHashMap<Integer, String>to store the(idKey, value)pairs that arrive. This allows for flexible, unordered insertion.ptr: An integer, initialized to1, that keeps track of the next ID we are expecting in the ordered sequence.
-
Constructor
OrderedStream(n):- Initializes an empty
HashMap. - Sets the
ptrto1.
- Initializes an empty
-
Method
insert(idKey, value):- The incoming
(idKey, value)pair is inserted into themap. - A new
ArrayListchunkis created to hold the output. - A
whileloop checks if themapcontains the key thatptris currently pointing to. - If it does, the corresponding value is added to the
chunk, andptris incremented to look for the next consecutive ID. - This process repeats until an ID is not found in the map, signifying a break in the contiguous sequence.
- The method then returns the
chunk. If the insertedidKeywas not the one expected byptr, the loop doesn't execute, and an empty list is returned.
- The incoming
import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map; class OrderedStream { private Map<Integer, String> map; private int ptr; public OrderedStream(int n) { map = new HashMap<>(); ptr = 1; } public List<String> insert(int idKey, String value) { map.put(idKey, value); List<String> chunk = new ArrayList<>(); while (map.containsKey(ptr)) { chunk.add(map.get(ptr)); ptr++; } return chunk; }}Complexity
Time
The constructor is `O(1)`. For `insert`, the `put` operation is `O(1)` on average. The `while` loop runs `k` times for a chunk of size `k`. The total work for the `while` loop across all `n` insertions is `O(n)`, making the amortized time complexity for `insert` `O(1)`. The worst-case time for a single call is `O(n)`.
Space
`O(n)` to store up to `n` key-value pairs in the HashMap.
Trade-offs
Pros
Simple to understand and implement.
Flexible for non-sequential or sparse keys, although not required by this specific problem.
Cons
Higher memory overhead compared to an array due to the HashMap's internal structure.
Hash map operations have an average time complexity of
O(1), which is not a worst-case guarantee like array access.
Solutions
Solution
class OrderedStream { private String [] data ; private int ptr ; public OrderedStream ( int n ) { data = new String [ n ]; ptr = 0 ; } public List < String > insert ( int idKey , String value ) { data [ idKey - 1 ] = value ; List < String > ans = new ArrayList <>(); while ( ptr < data . length && data [ ptr ] != null ) { ans . add ( data [ ptr ++]); } return ans ; } } /** * Your OrderedStream object will be instantiated and called as such: * OrderedStream obj = new OrderedStream(n); * List<String> param_1 = obj.insert(idKey,value); */Video walkthrough
Newsletter
One sharp idea, every week
System design and interview prep — short enough to finish.
No spam. Unsubscribe anytime.
Practice
Same difficulty — related problems to reinforce the pattern.