Hours Until Spoilage Spreads Through a Produce Cold Room

Last updated: November 29, 2026

Quick Overview

A cold room grid holds empty bins, fresh crates and spoiled crates. Each hour, spoilage spreads from every spoiled crate to fresh crates that share an edge. Return the number of hours until no fresh crate remains, or -1 if some crates can never be reached.

Walmart Labs
Coding & Algorithms
Software Engineer
Walmart Labs
November 29, 2026
Software Engineer
Online Assessment
Coding & Algorithms
Medium

0

0

0 solved


A cold room grid holds empty bins, fresh crates and spoiled crates. Each hour, spoilage spreads from every spoiled crate to fresh crates that share an edge. Return the number of hours until no fresh crate remains, or -1 if some crates can never be reached.

A multi-source BFS question themed on fresh produce handling in Walmart's grocery supply chain. In Walmart Labs online assessments the grader checks that you start BFS from all spoiled crates at once and count hours by levels, not by individual pops.

What the Interviewer Expects
  • Seed the queue with every initially spoiled crate
  • Process the queue level by level to count hours
  • Mark crates spoiled when enqueued to avoid duplicates
  • Count fresh crates to detect unreachable ones
  • Avoid mutating the caller's grid
Key Topics to Cover
multi-source BFS
grid problems
level-order traversal
simulation
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you report which crates would spoil last?
  • What if a refrigeration unit protects certain cells from spreading?
  • How would you handle spread that takes different times by produce type?
  • How would you compute this for a 3D grid of stacked pallets?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: a 2D list with 0 for an empty bin, 1 for a fresh crate, 2 for spoiled. Spread is four-directional and takes one hour per step. Output: hours until no fresh crate remains, 0 if none are fresh to...

High-Level Design

This is multi-source BFS: all spoiled crates spread at the same time, which is exactly what seeding the queue with all of them does. Process the queue one level at a time and increment the hour counte...


Submit Your Answer
Markdown supported