Data Structures & AlgorithmsHardTCS NQT Digital · Product Company SDE
Problem: Start with x = 5. Repeat this rule: if x is odd, set x = x + 3; if x is even, set x = 2x − 1. Count how many times the rule is applied until x first becomes at least 500. How many applications are needed?
- A. 12
- B. 7
- C. 13
- D. The value never reaches 500
Correct answer: A. 12
The trajectory is 5, 8, 15, 18, 35, 38, 75, 78, 155, 158, 315, 318, 635, so 12 applications. Using 2x instead of 2x − 1 doubles straight to 512 in 7 steps, counting the values (including the start) gives 13, and misreading the even rule as x / 2 traps x in the loop 8, 4, 2, 1, 4, …
More Coding Interview Problems questions
- Problem: Starting from 0 on a number line, a frog jumps +4, then −1, then +4, then −1 and so on, alternating, for 25 jumps in total. It…
- Problem: Count the vertices of the tree with edges 0–1, 0–2, 1–3, 1–4, 2–5, 5–6, 5–7 whose removal leaves every remaining connected…
- Problem: Vertices 1–6 have these directed prerequisites: 1→4, 2→4, 2→5, 3→5, 4→6, 5→6. Among all valid topological orders, the…
- Problem: A graph on vertices 0–7 has edges 0-1, 1-2, 2-0, 3-4, 4-5, 5-6, 6-3, 6-7. The edges are removed one at a time in this order: 2-0…
- Problem: In a graph with vertices 0–6, each edge has weight 0 or 1. Edges: 0-1 (0), 0-2 (1), 1-3 (1), 2-3 (0), 3-4 (1), 4-5 (0), 2-6 (1)…
- Problem: Vertices 0–3 with directed edges 0→1, 0→2, 1→2, 2→0, 1→3, 2→3, 3→3. How many directed walks of exactly length 3 start at vertex 0…