Reported September 2026
Amazonarray

Maximize Distance to the Closest Occupied Seat

Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Amazon OA. Under 2s to a working solution.
Founder's read

The edge case that breaks a naive solution is the one hiding at the ends of the row. This Amazon OA, reported in September 2026, asks you to pick the empty seat farthest from any occupied seat and return its index, smallest index on ties. Most people see gaps between occupied seats and forget that a run of zeros touching the wall counts differently. It's a single-pass array problem, and it's short. If you blank on the boundary logic during the live assessment, StealthCoder can run invisibly as a safety net while you work through it.

The problem

You are given an array seats, where seats[i] = 1 means seat i is occupied and seats[i] = 0 means it is empty. At least one seat is empty and at least one seat is occupied.
Choose an empty seat that maximizes its distance to the closest occupied seat, and return the zero-based index of that seat. If several seats have the same maximum distance, return the smallest index.

Function
bestSeat(seats: int[]) → int

Examples
Example 1
seats = [1,0,0,0,1,1]
return = 2
Seat 2 is two positions from the nearest occupied seat. Every other empty seat is only one position away.
Example 2
seats = [1,0,0,0]
return = 3
The last seat is three positions from the only occupied seat.
Example 3
seats = [0,1]
return = 0
Seat 0 is the only empty seat.
Example 4
seats = [1,0,0,1,0,0,1]
return = 1
Seats 1, 2, 4, and 5 all have nearest-person distance one, so the smallest index is returned.

Constraints
2 <= seats.length <= 20000
seats[i] is 0 or 1.
At least one seat is empty.
At least one seat is occupied.
If several empty seats have the same best distance, return the smallest index.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is treating three kinds of gaps separately. For leading zeros, the best seat is index 0 with distance equal to the count of leading zeros. For trailing zeros, the best seat is the last index with distance equal to the trailing zero count. For a gap between two occupied seats, the best seat is the middle, with distance (gap length + 1) / 2 using integer division, and the left middle on ties. Scan once, track the previous occupied index, and update the best only on strictly greater distance. That strictness gives you the smallest index for free, as long as you check left to right. The common pitfall is using (gap+1)/2 for the edges, which halves a distance that shouldn't be halved. Example 2, [1,0,0,0], catches it. StealthCoder is the hedge if the boundary cases slip your mind mid-assessment.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Maximize Distance to the Closest Occupied Seat cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as maximize distance to closest person. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Amazon's OA.

Amazon reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Maximize Distance to the Closest Occupied Seat FAQ

What's the trick in the Amazon closest occupied seat problem?+

Split the row into leading zeros, trailing zeros, and interior gaps. Edge gaps give full distance since only one side has a person. Interior gaps give half the gap, rounded down. Track the best with a strict greater-than comparison and scan left to right so ties keep the smallest index.

How hard is this one really?+

It's easy to medium. The algorithm is a single linear pass with no fancy data structure. What trips people up is the edge handling and the tie rule. If you test all four examples by hand before submitting, you'll catch nearly every bug.

Do I need two passes or can I do one?+

One pass works. Keep the index of the last occupied seat. When you hit a new occupied seat, compute the interior gap distance. Handle the leading zeros when you meet the first person, and the trailing zeros after the loop ends. It runs in O(n) time and O(1) space.

How do I handle ties for the smallest index?+

Scan left to right and only replace your best when the new distance is strictly greater. Example 4, [1,0,0,1,0,0,1], returns 1 because seat 1 is found first and seat 4 only matches it. For an interior gap, pick the left middle, which is prev + distance.

How do I prepare for this in 48 hours?+

Write the one-pass solution from memory twice. Then hand-run it on [0,1], [1,0,0,0], [0,0,0,1], and a case with equal gaps. Those four inputs cover every branch. Don't spend time on other patterns for this one, since it's pure array scanning.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Amazon.

OA at Amazon?
Invisible during screen share
Get it