PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates understanding of discrete grid-based simulations and local state-transition rules, focusing on modeling infection spread, neighbor-count constraints, and reasoning about convergence and stability in cellular-automata-like systems.

  • medium
  • Sealth
  • Coding & Algorithms
  • Software Engineer

Simulate Threshold Grid Infection

Company: Sealth

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given an `m x n` binary grid where `1` means infected and `0` means healthy, along with an integer `K`. The system evolves in discrete time steps. During each step, all updates happen simultaneously: - An infected cell stays infected. - A healthy cell becomes infected if and only if exactly `K` of its four orthogonal neighbors (`up`, `down`, `left`, `right`) are infected at the start of that step. - Neighbors outside the grid do not count. Write a function that returns the minimum number of time steps needed for all cells to become infected. If the process reaches a stable state and some cells are still healthy, return `-1`. You may assume the grid is non-empty.

Quick Answer: This question evaluates understanding of discrete grid-based simulations and local state-transition rules, focusing on modeling infection spread, neighbor-count constraints, and reasoning about convergence and stability in cellular-automata-like systems.

Simulate simultaneous infection steps until all cells are infected or the state becomes stable.

Constraints

  • Inputs are Python literals matching the function signature.
  • Return a deterministic exact-match value.

Examples

Input: ([[1,0],[0,0]], 1)

Expected Output: -1

Explanation: Infection spreads one-neighbor cells first.

Input: ([[1,0],[0,0]], 4)

Expected Output: -1

Explanation: Stable with healthy cells returns -1.

Input: ([[1,1]], 1)

Expected Output: 0

Explanation: Already all infected takes zero steps.

Hints

  1. Clarify edge cases before coding.
  2. Keep the return value deterministic.
Last updated: Jun 27, 2026

Related Coding Questions

  • Implement one-cluster k-means - Sealth (easy)
  • Decode Ambiguous Morse Messages - Sealth (medium)

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.