Problem: Time taken by tomatoes to rot in JavaScript

Problem

We are required to write a JavaScript function that takes in a 2-D array of numbers, arr, as the only argument.

The numbers in the array can be:

  • the value 0 which represents an empty cell;

  • the value 1 which represents a fresh tomato;

  • the value 2 which represents a rotten tomato.

Every minute, any fresh tomato that is adjacent (4-directionally) to a rotten tomato becomes rotten.

Our function is supposed to return the minimum number of minutes that must elapse until no cell has a fresh tomato. If this is impossible, we should return -1 instead.

For example, if the input to the function is:

const arr = [
    [2, 1, 1],
    [1, 1, 0],
    [0, 1, 1]
];

Then the output should be:

4

Output Explanation

The tomatoes rot progressively over 4 minutes as shown below:

Time Elapsed Tomatoes Status
1
[
    [2, 2, 1],
    [2, 1, 0],
    [0, 1, 1]
]
                
2
[
    [2, 2, 2],
    [2, 2, 0],
    [0, 1, 1]
]
                
3
[
    [2, 2, 2],
    [2, 2, 0],
    [0, 2, 1]
]
                
4
[
    [2, 2, 2],
    [2, 2, 0],
    [0, 2, 2]
]
                

Algorithm Approach

This problem uses a Breadth-First Search (BFS) approach:

  • Count all fresh tomatoes and collect initial rotten tomato positions

  • For each minute, spread rot from current rotten tomatoes to adjacent fresh ones

  • Continue until no more tomatoes can rot

  • Return minutes elapsed if all fresh tomatoes rot, otherwise return -1

Example

const arr = [
    [2, 1, 1],
    [1, 1, 0],
    [0, 1, 1]
];

const timeToRot = (arr = []) => {
    let fresh = 0;
    let count = -1;
    let curr = [];
    
    // Count fresh tomatoes and find initial rotten positions
    for(let i = 0; i  0){
        count += 1;
        const next = [];
        
        const rotten = (i, j) => {
            arr[i][j] = 2
            next.push([i, j])
            fresh -= 1
        };
        
        // Check 4 directions for each currently rotten tomato
        for(const [i, j] of curr){
            if (arr[i - 1] && arr[i - 1][j] === 1) {
                rotten(i - 1, j);
            };
            if (arr[i + 1] && arr[i + 1][j] === 1) {
                rotten(i + 1, j);
            };
            if (arr[i][j - 1] === 1) {
                rotten(i, j - 1);
            };
            if (arr[i][j + 1] === 1) {
                rotten(i, j + 1);
            };
        }
        curr = next
    };
    
    return fresh === 0 ? count : -1;
};

console.log(timeToRot(arr));

Output

4

Key Points

  • Uses BFS to simulate simultaneous spreading of rot

  • Checks all 4 directions (up, down, left, right) for adjacent fresh tomatoes

  • Returns -1 if some fresh tomatoes remain unreachable

  • Time complexity: O(m × n) where m and n are grid dimensions

Conclusion

This BFS solution efficiently calculates the minimum time for all tomatoes to rot by simulating the spreading process minute by minute. It handles edge cases like isolated fresh tomatoes by returning -1 when complete rot is impossible.

Updated on: 2026-03-15T23:19:00+05:30

208 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements