Data Structures & AlgorithmsHardProduct Company SDE · Amazon OA

Problem: A grid holds oranges: 2 = rotten, 1 = fresh, 0 = empty: [[2,1,1],[1,1,0],[0,1,1]]. Each minute every fresh orange adjacent (4-directionally) to a rotten one becomes rotten. What is the minimum number of minutes until no fresh orange remains, or −1 if impossible? Report the number of minutes (the answer is non-negative here).

  1. A. 5
  2. B. 4
  3. C. 3
  4. D. 6

Correct answer: B. 4

Multi-source BFS from all rotten oranges at time 0 gives the time each fresh orange rots. Running one source at a time gives the wrong total.

More Coding Interview Problems questions

Exams that ask this topic