Data Structures & AlgorithmsHardProduct-company placement
In the grid 21112 / 11110 / 10111 / 11111 (rows top to bottom; 2 = rotten, 1 = fresh, 0 = empty), every minute each rotten orange rots its fresh 4-neighbours. After how many minutes are no fresh oranges left?
- A. 7
- B. 5
- C. 4
- D. 8
Correct answer: B. 5
This is a multi-source BFS: put ALL rotten cells in the queue at time 0; the answer is the largest BFS distance to a fresh cell, 5. Running BFS from one rotten orange only overstates it.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- An undirected graph has edges A–E, A–H, B–F, C–E, C–G, D–G, D–H, E–F, E–G, F–H. BFS starts at A and always scans neighbours in…
- A tree on vertices 1–10 has edges 2–1, 3–2, 4–1, 5–4, 6–1, 7–3, 8–7, 9–5, 10–9. What is the size of a minimum DOMINATING set (every vertex…
- What is the length of the longest common subsequence of CONTEST and RECESSION, and how many DISTINCT longest common subsequences (as…
- A tree on vertices 1–8 has edges 2–1, 3–2, 4–1, 5–2, 6–5, 7–2, 8–1. What is the sum of the distances (in edges) over all unordered pairs…
- How many structurally different binary trees with 7 nodes have height EXACTLY 4 (height = edges on the longest root-to-leaf path)?
- Keys 54, 17, 39, 66, 11, 36, 83, 51, 94, 10, 76 are inserted in that order into an empty BST. Then 39 is deleted and after that 83 is…