Choose an Empty Seat with the Best Nearest-Neighbor Distance

Quick Overview

Choose an Empty Seat with the Best Nearest-Neighbor Distance. seats marks occupied positions with 1 and empty positions with 0.

Choose an Empty Seat with the Best Nearest-Neighbor Distance

Company: Yandex

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem seats marks occupied positions with 1 and empty positions with 0. Choose an empty position to maximize its index distance to the closest occupied position. Return that maximum distance, not the chosen seat index. ```cpp class Solution { public: int maxDistToClosest(vector<int>& seats); }; ``` ### Examples **Example 1:** ```text seats = [0,0,1,0,0,0,1] Output: 2 Explanation: Seat0 is two positions from the nearest occupant; the interior gap also permits distance2. ``` **Example 2:** ```text seats = [1,0,0,1] Output: 1 Explanation: Both empty positions have a nearest occupant one position away. ``` ### Constraints - Row length lies in [2,20000]. - Entries are 0 or 1, with at least one of each. ### Contract considerations Return distance and retain the existence of an empty seat. ```hint Read the boundary condition How do edge gaps differ from gaps bounded by occupants on both sides? ``` ### Boundary reminders Return distance and retain the existence of an empty seat.

Overview: Choose an Empty Seat with the Best Nearest-Neighbor Distance. seats marks occupied positions with 1 and empty positions with 0.

Yandex logo
Yandex
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

seats marks occupied positions with 1 and empty positions with 0. Choose an empty position to maximize its index distance to the closest occupied position. Return that maximum distance, not the chosen seat index.

class Solution {
public:
    int maxDistToClosest(vector<int>& seats);
};

Examples

Example 1:

seats = [0,0,1,0,0,0,1]
Output: 2
Explanation: Seat0 is two positions from the nearest occupant; the interior gap also permits distance2.

Example 2:

seats = [1,0,0,1]
Output: 1
Explanation: Both empty positions have a nearest occupant one position away.

Constraints

  • Row length lies in [2,20000].
  • Entries are 0 or 1, with at least one of each.

Contract considerations

Return distance and retain the existence of an empty seat.

Boundary reminders

Return distance and retain the existence of an empty seat.