Quant Memo
Coding/●●●●●

One id duplicated, one missing, find both

A settlement batch should contain every id from 11 to nn exactly once. Because of a bug, one id got written twice and, as a result, a different id is entirely missing. The array still has length nn.

ids = [1, 2, 2, 4]      # range is 1..4: id 2 is duplicated, id 3 is missing
-> (2, 3)               # (duplicated, missing)

Return the duplicated id and the missing id in O(n)O(n) time and O(1)O(1) space.

Show a hint

XOR the array against the full range 1..n1..n. You get dup XOR missing. How do you split two survivors apart?

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions