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 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.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 == 5value consists only of lowercase letters.insert will have a unique id.n calls will be made to insert.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The Ordered Stream problem requires us to assemble a sequence of values in the correct order based on their positions. A brute-force approach involves repeatedly checking for the next expected value in the stream and adding it to our result if it's available.
Here's how the algorithm would work step-by-step:
class OrderedStream:
def __init__(self, stream_size: int):
self.stream_array = [None] * (stream_size + 1)
self.pointer = 1
def insert(self, id_key: int, value: str) -> list[str]:
self.stream_array[id_key] = value
result = []
# Keep adding values to result while in order
while self.pointer < len(self.stream_array) and self.stream_array[self.pointer] is not None:
result.append(self.stream_array[self.pointer])
self.pointer += 1
return resultThe goal is to process a stream of data in order. Instead of immediately returning data as it arrives, we hold onto it until we have a continuous sequence starting from the expected position. This allows us to efficiently output the ordered stream.
Here's how the algorithm would work step-by-step:
class OrderedStream:
def __init__(self, number_of_values):
self.data_store = {}
self.current_position = 1
def insert(self, id_key, value):
self.data_store[id_key] = value
output = []
# Check if the current id matches expected position
if id_key == self.current_position:
# Extract and append continuous sequence
while self.current_position in self.data_store:
output.append(self.data_store[self.current_position])
self.current_position += 1
# We increment the position only when a continuous sequence is extracted
return output| Case | How to Handle |
|---|---|
| Empty input stream | Initialize the internal data structure to an appropriate empty state and return an empty list for any `insert` call. |
| Input `idKey` less than 1 | Throw an IllegalArgumentException or return an empty list as invalid idKey leads to out-of-bounds access if used as index. |
| Input `idKey` greater than stream length | Throw an IllegalArgumentException or return an empty list because this is an invalid insertion location beyond the stream's capacity. |
| Maximum stream length reached (consider memory constraints) | Check if the stream has reached its maximum capacity before inserting; if so, either resize (if allowed) or throw an exception to prevent memory overflow. |
| Inserting out-of-order, but contiguous elements | Store the value at the given index; when the pointer reaches the stored index, start returning the contiguous chunk. |
| Inserting out-of-order and non-contiguous elements (gaps in the ordered stream) | Store the inserted values and only return a chunk when the current pointer finds the start of a contiguous block of non-null values. |
| Null value being inserted | Store the null value in the array and skip it when returning chunks as per expected behavior (depending on requirements, it might be better to throw an exception). |
| Large number of inserts that don't release any data. | The solution should handle a continuous stream of inserts where `ptr` never advances, potentially indicating a problem with the input order; this can lead to memory buildup and the solution should scale efficiently by managing memory appropriately. |