Merge two ascending arrays of numbers into one ascending array without sorting the final result and without changing either input array.
Requirements
Do not call Array.sort
Do not mutate the inputs
Keep duplicate values
Example
Input
{
"left": [
1,
4,
8
],
"right": [
2,
3,
9
]
}
Expected output
[
1,
2,
3,
4,
8,
9
]
Show a hint
Write down the input contract and the failure cases before coding.
tests
RUNTIME OUTPUT
Ready to run the file open above against 2 tests.
Runs the open file in a restricted browser worker
LEARN FROM THE SOLUTION
Why the solution works.
Each pointer advances once, so every value is inspected at most once. When one input ends, the unvisited suffix of the other input is already sorted and can be appended without another comparison or a global sort.
What this exercise teaches
Two pointers
Loop invariants
Algorithmic complexity
A PRACTICAL PLAN
Work through Merge two sorted result lists with intent.
Translate the contract.Turn the requirements into a short checklist before editing your-solution.js.
Use the example as evidence.Predict the result for the supplied input, then add one boundary case such as an empty value, a limit, or unexpected input.
Review the implementation.Compare your choices against solution.js only after a real attempt.
EXERCISE FAQ
Before you move on.
What does “Merge two sorted result lists” teach?
This intermediate JavaScript exercise focuses on Two pointers, Loop invariants, Algorithmic complexity. Its requirements define the exact behavior to implement before you write code.
How should I validate this JavaScript solution?
Start with the displayed example input and expected output, then test a boundary case suggested by the requirements. Open your-solution.js and select Run code. The browser executes that file in an isolated worker against the visible test cases.
When should I open the reference solution?
Attempt Merge two sorted result lists first. Then open the read-only solution file to compare the contract, edge-case handling, and implementation choices—not simply to copy the final code.