Design an Ordered Stream

Too many downvotes. Bad problem description. Do not waste time on this one.

Here is a Python solution for the problem. This solution uses an array to store the values, and an integer to keep track of the current position. When an insertion is made, it checks if the inserted key is at the current position, if so, it adds all the continuous values to the result until it encounters an empty position.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class OrderedStream:
    def __init__(self, n: int):
        self.data = [''] * (n + 1)  # Initialize the stream with empty strings
        self.ptr = 1  # Pointer to track the current position

    def insert(self, idKey: int, value: str) -> list[str]:
        self.data[idKey] = value  # Insert the value at the idKey position
        res = []
        # If the inserted idKey is at the current position,
        # add all the continuous values to the result until an empty position is found.
        while self.ptr < len(self.data) and self.data[self.ptr] != '':
            res.append(self.data[self.ptr])
            self.ptr += 1
        return res

In the insert method, we first insert the value at the correct position. Then, we check if the current position of the pointer is equal to the inserted idKey. If it is, we add all the continuous values in the stream to the result, updating the pointer to the new position where the next value is expected.

This way, each time insert is called, it returns the largest possible chunk of currently inserted values that appear next in order.