Data Structures & AlgorithmsModerateGATE CS · TCS NQT

In a grid, 2 marks a rotten orange, 1 a fresh one and 0 an empty cell. Each minute every fresh orange adjacent (4 directions) to a rotten one becomes rotten. The grid is [[2,1,1],[1,1,0],[0,1,1]]. How many minutes until no fresh orange remains?

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

Correct answer: C. 4

A multi-source breadth-first search starts from all rotten oranges at once; the number of BFS layers that infect something is the answer.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic