Data Structures & AlgorithmsVery hardICPC regional · CAT · MBA placement

Problem: XOR of all integers from l = 13 to r = 46 inclusive. Compute the result.

  1. A. 13
  2. B. 47
  3. C. 46
  4. D. 35

Correct answer: D. 35

Use f(n) = XOR of 1..n, which cycles n, 1, n + 1, 0 by n mod 4; the answer is f(46) ^ f(12) = 47 ^ 12 = 35. Treating r as exclusive gives 13, forgetting to cancel 1..12 gives f(46) = 47, and the classic slip f(r) ^ f(l) instead of f(l − 1) gives 46.

More Coding Interview Problems questions

Exams that ask this topic