Place one delivery center to minimize the worst Chebyshev distance

Quick Overview

Minimize a delivery grid’s worst nearest-center distance by adding at most one center. Use Chebyshev geometry and handle grids with no centers, remaining demand cells, and the zero-inconvenience boundary.

Place one delivery center to minimize the worst Chebyshev distance

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Online Assessment

A binary grid contains delivery centers at cells with value 1 and unserved locations at cells with value 0. The distance between `(r1, c1)` and `(r2, c2)` is the Chebyshev distance `max(abs(r1-r2), abs(c1-c2))`. The grid's inconvenience is the largest, over all zero cells, of the distance to the nearest center. You may convert at most one zero cell into a center. Return the smallest possible inconvenience afterward. ### Function Signature ```python def minimum_inconvenience(grid: list[list[int]]) -> int: ``` ### Rules - Every existing center remains in place. - A newly added center is no longer a zero cell and is excluded from the remaining demand locations. - If no zero cells remain, inconvenience is 0. - A grid may initially have no centers. In that case, place a center so that every remaining zero cell has a finite nearest-center distance. - Return the minimum inconvenience only. No placement tie-break is needed. ### Constraints These bounds and empty-set conventions define this practice version. - The grid has between 1 and 300 rows and between 1 and 300 columns. - Every row has the same length and every cell is 0 or 1. - Distances are nonnegative integers at most 299 after a center exists. ### Examples ```text Input: grid = [[0, 0, 0]] Output: 1 ``` Place a center in the middle. Each remaining zero cell is at distance 1. ```text Input: grid = [[1, 0], [0, 0]] Output: 1 ``` The existing center is within distance 1 of every zero cell, including the diagonal cell. One conversion cannot remove all zero cells, so inconvenience cannot become 0. ```text Input: grid = [[0]] Output: 0 ``` Converting the only cell leaves no zero cells.

Overview: Minimize a delivery grid’s worst nearest-center distance by adding at most one center. Use Chebyshev geometry and handle grids with no centers, remaining demand cells, and the zero-inconvenience boundary.

Read the full Amazon Software Engineer interview experience this question came from

Amazon logo
Amazon
Nov 27, 2026
mediumSoftware EngineerOnline AssessmentCoding & Algorithms
0
0

A binary grid contains delivery centers at cells with value 1 and unserved locations at cells with value 0. The distance between (r1, c1) and (r2, c2) is the Chebyshev distance max(abs(r1-r2), abs(c1-c2)).

The grid's inconvenience is the largest, over all zero cells, of the distance to the nearest center. You may convert at most one zero cell into a center. Return the smallest possible inconvenience afterward.

Function Signature

def minimum_inconvenience(grid: list[list[int]]) -> int:

Rules

  • Every existing center remains in place.
  • A newly added center is no longer a zero cell and is excluded from the remaining demand locations.
  • If no zero cells remain, inconvenience is 0.
  • A grid may initially have no centers. In that case, place a center so that every remaining zero cell has a finite nearest-center distance.
  • Return the minimum inconvenience only. No placement tie-break is needed.

Constraints

These bounds and empty-set conventions define this practice version.

  • The grid has between 1 and 300 rows and between 1 and 300 columns.
  • Every row has the same length and every cell is 0 or 1.
  • Distances are nonnegative integers at most 299 after a center exists.

Examples

Input: grid = [[0, 0, 0]]
Output: 1

Place a center in the middle. Each remaining zero cell is at distance 1.

Input: grid = [[1, 0], [0, 0]]
Output: 1

The existing center is within distance 1 of every zero cell, including the diagonal cell. One conversion cannot remove all zero cells, so inconvenience cannot become 0.

Input: grid = [[0]]
Output: 0

Converting the only cell leaves no zero cells.