Coding interview questionMedium
AmazonMicrosoft

Minimum Time for Contamination to Spread

Given a grid where each cell is empty, clean, or already contaminated, find the minimum time for all clean cells to become contaminated, spreading one step per unit time to adjacent cells, or report it is impossible.

1. Problem Statement

You are given a grid where each cell is empty (0), clean (1), or contaminated (2). Every minute, contamination spreads to horizontally or vertically adjacent clean cells. Return the minimum number of minutes for every clean cell to become contaminated, or -1 if some clean cell can never be reached.

2. Live Coding Format

Work in the provided coding workspace while explaining your decisions to the live interviewer. Validation may use inline tests, examples, comments, or a verbal walkthrough.

3. Key Focus Areas

  • 1
    Problem clarification and contract
  • 2
    Approach exploration and optimality
  • 3
    Think-aloud communication and pacing
  • 4
    Implementation correctness
  • 5
    Code quality and language fluency
  • 6
    Tests and dry run
  • 7
    Debugging and self-correction
  • 8
    Complexity analysis
  • 9
    Follow-up performance

4. What Strong Candidates Should Demonstrate

  • Clarify cell states, spread direction rules, and impossible-case behavior before coding.
  • Explain a round-by-round full-grid rescan baseline and improve it to a linear-time solution.
  • Think aloud while implementing readable, correct code.
  • Validate with explicit expected outputs and a top-to-bottom dry run.
  • Analyze time and space complexity and handle a bounded follow-up modification.

Want interactive feedback?

Practice this problem in the live coding workspace while the interviewer probes your clarification, implementation, trade-offs, and validation.

Continue to Dashboard

Core Concepts

Hidden because they name the technique, which a real interviewer would not tell you.

Show core concepts
Grid TraversalMulti-Source BFSQueuesComplexity Analysis

Continue preparing

Build a complete software engineer mock interview plan

Related Coding / DSA

Determine a Valid Course Completion Order

Given a number of courses and a list of prerequisite pairs, determine a valid order to complete all courses, or report that no valid order exists.