Implement function mergeSortedIterators(it1: ()=>number null, it2: ()=>number null):…
- User
Implement
function mergeSortedIterators(it1: ()=>number|null, it2: ()=>number|null): number[]merging two sorted streams. Assistant
We'll write a function in TypeScript (or JavaScript) that:
- Calls both iterator functions to get their first values.
- While both iterators still have values, compare them, push the smaller, and advance that iterator.
- Once one iterator is exhausted, push all remaining values from the other.
- Return the merged array.
Edge cases: both empty, one empty, duplicate values.
Time complexity: O(n + m) where n and m are the lengths of the streams. Space: O(1) extra space (excluding the result array).
We'll provide the code with type annotations as required.
typescript