Data Structures & AlgorithmsModerateICPC regional · TCS NQT
Problem: The quadratic residues modulo 8: how many integers x in 1 ≤ x ≤ 100 satisfy x² ≡ 1 (mod 8)?
- A. 50
- B. 13
- C. 25
- D. 100
Correct answer: A. 50
Every odd square is 1 mod 8 (odd x = 2m + 1 gives 4m(m + 1) + 1, and m(m + 1) is even), so all 50 odd numbers qualify. Counting only x ≡ 1 (mod 8) gives 13, only x ≡ ±1 (mod 8) gives 25, and 100 forgets that even squares are 0 or 4 mod 8.
More Coding Interview Problems questions
- Problem: Dijkstra's shortest distances from source 0 in the directed graph 0→1 (3), 0→2 (1), 2→1 (1), 1→3 (2), 2→3 (6), 3→4 (1), 1→4 (9)…
- Problem: The number of paths from the bottom-left to the top-right corner of a 4×4 grid of points, moving only right, up, or diagonally…
- Problem: Find the number of inversions of the sequence 2, 4, 1, 3, 5, 0 processed with merge sort (count each cross-pair during merging).
- Problem: A linked list is written as values 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8. The list is reversed in groups of 3 (the last group of fewer…
- Problem: The regex-like string match: count the substrings of "aabbaab" that match the pattern a+b (one or more a's immediately followed…
- Problem: Starting from the tree with root 1 and children 1: [2, 3], 2: [4], 3: [5, 6], 5: [7], a DFS visits children in increasing order…