Quant Memo
Coding/●●●●●

Find the missing sequence number with XOR

An exchange feed stamps each message with a sequence number. A clean session should contain every number from 00 to nn exactly once, but one message was dropped, so the array of received numbers has length nn and is missing exactly one value from that range.

received = [3, 0, 1]        # range is 0..3, so 2 is missing
-> 2

Find the missing sequence number in O(n)O(n) time and O(1)O(1) space. Summing and subtracting works but can overflow on huge ranges; the interviewer wants the XOR version.

Show a hint

If you XOR together every index 0,1,,n0, 1, \dots, n and every received value, what cancels?

Your answer

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

More Coding questions