Problem, as an interviewer would state it: "Merge two sparse arrays (arrays with holes) so that the cost depends on how many elements are actually present, not on the array length. Discuss edge cases, runtime, and alternatives."
Examples:
a = [ , "a1", , , "a4", ] // length 6, holes at 0, 2, 3, 5
b = [ , "b1", "b2" ] // length 3
merge(a, b) -> [ , "b1", "b2", , "a4", ] // length 6; b wins where it has a value
Clarifying questions expected:
- When both arrays have a value at an index, which wins? (This solution:
boverridesa.) - Should holes stay holes in the result, and how long should the result be?
- Is an explicit
undefineda real value or the same as a hole?
Code / implementation expected: Yes — real, direct proof against a dense reference, and a measurement of which traversals genuinely avoid walking every index.