Data Structures & AlgorithmsHardProduct Company SDE · ICPC regional warm-up
Problem: A runner moves up, down, left or right one cell at a time on this 5×5 grid. '.' is open, '#' is a wall. The runner starts at (row 0, col 0) and must reach (row 4, col 4), and may pass through at most ONE wall cell (entering it costs one move like any other step). ..#.. .##.# ...#. #...# ...#. What is the minimum number of moves?
- A. 8
- B. 9
- C. No route exists
- D. 10
Correct answer: A. 8
BFS over states (cell, walls used) finds an 8-move route that breaks one wall, e.g. down the left side and through (4,3), matching the Manhattan distance. Without the break the goal is sealed off, and a BFS whose visited set ignores the walls-used state also wrongly reports no route; counting cells instead of moves gives 9, and also charging an extra move for the break gives 10.
More Coding Interview Problems questions
- Problem: An undirected road network has 5 towns numbered 0 to 4 and these roads with lengths: 0–1 (7), 0–2 (2), 2–1 (3), 1–3 (1), 2–3 (8)…
- Problem: Nine nodes 0 to 8 start with no edges. These union operations are applied in order: (0,1), (2,3), (1,2), (4,5), (6,7), (7,8)…
- Problem: A tree has 8 nodes 0–7 and edges: 0–1, 1–2, 1–3, 3–4, 4–5, 3–6, 6–7. What is the diameter of the tree, measured as the number of…
- Problem: A directed graph has vertices 0–6 and arcs: 0→1, 1→2, 2→0, 2→3, 3→4, 4→5, 5→3, 5→6, 6→6. A vertex lies on a cycle if it can reach…
- Problem: Count how many of these four undirected graphs are bipartite (2-colourable so that every edge joins different colours): G1: edges…
- Problem: Find the length of the longest palindromic substring of the string "abacdfgdcabazyx".