The Find Closest Person coding problem typically involves a row of seats or a 1D grid where some seats are occupied and others are empty. You are given the location of an empty seat and need to find the distance to the nearest person (occupied seat). Alternatively, you might be asked to find an empty seat that maximizes the distance to the nearest person.
Meta and Amazon use this problem to test your ability to calculate distances in a linear space efficiently. It evaluation your proficiency with Array interview patterns, specifically involving Multi-pass scans or Two Pointers. It mimics real-world scenarios like social distancing or resource allocation in a distributed system.
This problem is best solved using a Prefix and Suffix Scan.
Seats: [1, 0, 0, 0, 1] (1 = person, 0 = empty)
[0, 1, 2, 3, 0] (distance from left-most person).[0, 3, 2, 1, 0] (distance from right-most person).[0, 1, 2, 1, 0].
The middle seat (index 2) is the farthest from anyone, with a "closest person distance" of 2."Closest element" problems in 1D arrays are often solved by two passes (left-to-right and right-to-left). This is a foundational Prefix Sum interview pattern variation that avoids the need for complex search algorithms.