Back to Amazon
Amazon logo

Coding rounds

Amazon coding rounds

5 timed coding challenges drawn from real loops. Submit a solution to see your test pass rate and AI feedback.

All coding rounds
intermediate 45:00+100 XP

Rotting Oranges

In an `m x n` grid each cell is 0 (empty), 1 (fresh orange), or 2 (rotten orange). Every minute, any fresh orange adjacent (4-directionally) to a rotten one becomes rotten. Return the minimum minutes until no fresh orange remains, or -1 if impossible. Multi-source BFS.

Examples

  • spreads in 4 minutes

    in: [[2,1,1],[1,1,0],[0,1,1]]

    out: 4

  • bottom-left orange unreachable

    in: [[2,1,1],[0,1,1],[1,0,1]]

    out: -1

javascript
Loading editor...