Data Structures & AlgorithmsHardTCS NQT Digital · Product Company SDE
Problem: A dice-throwing game: starting at 0, each throw adds a value from 1 to 6, and the player stops as soon as the total is at least 20. How many throw sequences (ordered) end exactly at a total of 20?
- A. 233904
- B. 920319
- C. 463968
- D. 524288
Correct answer: C. 463968
Every partial sum before the last throw is below 20, so the sequences are exactly the compositions of 20 into parts 1-6: f(n) = f(n − 1) + … + f(n − 6) gives 463968. An off-by-one total gives 233904 (19) or 920319 (21), and allowing parts of any size gives 2¹⁹ = 524288.
More Coding Interview Problems questions
- Problem: Dijkstra is run on a directed graph with arcs 0→1 (5), 0→2 (2), 2→1 (1), 1→3 (2), 2→3 (7), 3→4 (1), 2→4 (9). Each vertex is…
- Problem: A directed graph on vertices 0–4 has arcs 0→1, 1→2, 2→0, 2→3, 3→4. How many distinct simple directed cycles does it contain?
- Problem: An undirected graph on 6 vertices has edges 0-1, 1-2, 2-3, 3-0, 3-4, 4-5. Find the number of vertices whose removal leaves a…
- Problem: The rooted tree (root 0) has children 0: [1, 2], 1: [3, 4], 2: [5], 4: [6, 7], 5: [8]. Nodes are numbered in DFS pre-order with…
- Problem: A binary tree is given by a parent array where node i has parent p[i] (p[0] = −1 is the root): p = [−1, 0, 0, 1, 1, 2, 2, 5]. How…
- Problem: What is the minimum number of swaps of two neighbouring characters needed to turn "abcdef" into "fedcba"?