Python solution (based on heap)
\nimport heapq\n\nclass OrderedStream:\n\n    def __init__(self, n: int):\n        self.idx = 1\n        self.heap = []\n\n    def insert(self, idKey: int, valu
```\nimport heapq\n\nclass OrderedStream:\n\n    def __init__(self, n: int):\n        self.idx = 1\n        self.heap = []\n\n    def insert(self, idKey: int, value: str) -> List[str]:\n        result = []\n        \n        if idKey == self.idx:\n            result.append(value)\n            self.idx += 1\n            \n            while self.heap and self.heap[0][0] == self.idx:\n                result.append(heapq.heappop(self.heap)[1])\n                self.idx += 1\n        else:\n            heapq.heappush(self.heap, (idKey, value))\n            \n        return result\n```
['Heap (Priority Queue)', 'Python']