Java O(N) time O(N) space, with explanation in code
\n// NOTE: \n// To understand the question, the concatenation of all the chunks should result in a list of the SORTED values.\n// That is why calling the functi
```\n// NOTE: \n// To understand the question, the concatenation of all the chunks should result in a list of the SORTED values.\n// That is why calling the function of insert(3, "ccccc") will NOT return anything. \n// In other words, calling insert(1, "ccccc") returns "ccccc"\nclass OrderedStream {\n\n    private String[] record;\n\t\n\t//The starting index  of the next output when insert() is called\n    private int outputIndex;  \n\t\n    public OrderedStream(int n) {\n        // Store current (idKey, Value) into this array\n        record = new String[n + 1];\n        Arrays.fill(record, "");\n        \n        // The outputIndex is the starting index of the chunk, which is to be \n        // returned via "insert" function. We will need to update outputIndex accordingly\n        outputIndex = 1;\n    }\n    \n    public List<String> insert(int idKey, String value) {\n        List<String> result = new ArrayList<>();\n        \n        // Regardless of idKey equals to outputIndex or not, need to store current (idKey, Value)\n        record[idKey] = value;\n        \n        if (idKey == outputIndex) {\n            int i = outputIndex;\n            while (i < record.length && record[i] != "") {\n                result.add(record[i]);\n                ++i;\n            }\n            // outputIndex has scanned the current continuous non-empty value,\n            // the next available value is i. \n            // Otherwise, it violates "The concatenation of all the chunks should \n            // result in a list of the sorted values."\n            outputIndex = i;\n        }\n        return result;\n    }\n}\n ```
[]