Data Structures & AlgorithmsVery hardICPC regional · CAT · MBA placement
Problem: XOR of all integers from l = 13 to r = 46 inclusive. Compute the result.
- A. 13
- B. 47
- C. 46
- 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
- Problem: The number of trailing zeros of 500! written in base 6 (i.e., how many times 6 divides 500!) is what value?
- Problem: A 5×5 grid has 1s and 0s: 11000 / 11011 / 00100 / 01100 / 10001 (rows top to bottom). You may flip exactly one 0 into a 1. What…
- Problem: A DAG with vertices 0–5 has weighted edges 0→1 (2), 0→2 (1), 1→3 (2), 2→3 (3), 2→4 (1), 3→5 (1), 4→5 (5). How many distinct…
- Problem: An undirected connected graph has 9 vertices and 13 edges. What is the cyclomatic number (the number of independent cycles…
- Problem: An undirected graph on 7 vertices (0–6) has degrees 0:3, 1:2, 2:4, 3:1, 4:2, 5:3, 6:1. How many vertices have odd degree? (Such a…
- Problem: Vertices 0–5 with undirected edges 0-1, 1-2, 2-0, 2-3, 3-4, 4-5, 5-3. How many articulation points (cut vertices, whose removal…