Problem 48 of 75

Rotting Oranges

MediumNot solved yetInterview me on this
Before you code · which pattern fits?

The problem

In a grid, 0 = empty, 1 = fresh orange, 2 = rotten orange. Every minute, fresh oranges 4-directionally adjacent to a rotten one become rotten. Return the minimum minutes until no fresh orange remains, or -1 if impossible.

[[2,1,1],[1,1,0],[0,1,1]] → 4 [[2,1,1],[0,1,1],[1,0,1]] → -1

Your solution runs against 3 unit tests, including edge and large-value cases.

Loading...

Test console · 3 unit tests

Submit your code to run it against all 3 unit tests. Results appear here.

JavaScript, TypeScript & Python run sandboxed in your browser; other languages run on the execution server. Your code is saved locally as you type.

Hint

Reference solution

Video explanation

Rotting Oranges, explainedNeetCode