Nearest Eligible Elevator
Reported by candidates from Pinterest's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt on this Pinterest OA, reported September 2026, is picking the closest elevator and checking direction later. Nearest Eligible Elevator looks like a warm-up, and it is. It's a single linear scan with a filter and a tie-break. But the eligibility rules have three branches, and candidates who skim them fail the hidden cases. Example 2 is the trap: an elevator sits right on the passenger's floor and still doesn't qualify. If you've got an OA coming, read the rules twice, then code. StealthCoder is there as a safety net if your mind goes blank mid-assessment.
The problem
You are given two parallel arrays describing a group of elevators: floors[i] is the current floor of elevator i. states[i] is "UP", "DOWN", or "IDLE". A passenger is waiting at passengerFloor and wants to travel in passengerDirection, which is either "UP" or "DOWN". An elevator is eligible under these rules: An idle elevator can serve any passenger. An elevator moving up can serve the passenger only when the passenger also wants to go up and the elevator is at or below passengerFloor. An elevator moving down can serve the passenger only when the passenger also wants to go down and the elevator is at or above passengerFloor. Return the index of the eligible elevator with the smallest absolute floor distance from the passenger. For this exercise, assume equally distant eligible elevators are ordered by their smaller original index. If no elevator is eligible, return -1. Function nearestEligibleElevator(floors: int[], states: String[], passengerFloor: int, passengerDirection: String) → int Examples Example 1 floors = [2,8,12] states = ["UP","IDLE","DOWN"] passengerFloor = 6 passengerDirection = "UP" return = 1 Elevator 0 is moving up toward the passenger and is 4 floors away. Elevator 1 is idle and is 2 floors away. Elevator 2 is moving in the opposite direction. Therefore, elevator 1 is the nearest eligible elevator. Example 2 floors = [3,9,9,15] states = ["UP","DOWN","IDLE","DOWN"] passengerFloor = 9 passengerDirection = "UP" return = 2 Elevator 1 is already at floor 9, but it is moving down while the passenger wants to go up, so it is ineligible. Elevator 2 is idle at the passenger's floor and is selected. Example 3 floors = [3,7,12] states = ["IDLE","IDLE","UP"] passengerFloor = 5 passengerDirection = "UP" return = 0 Elevators 0 and 1 are both idle and both are 2 floors away. The smaller-index tie rule selects elevator 0. Example 4 floors = [8,2] states = ["UP","DOWN"] passengerFloor = 5 passengerDirection = "DOWN" return = -1 Elevator 0 is moving in the wrong direction. Elevator 1 is moving down from below the passenger, so the passenger is not along its current path. No elevator is eligible. Constraints floors.length == states.length Each value in states is "UP", "DOWN", or "IDLE". passengerDirection is "UP" or "DOWN". Every floor is a 32-bit signed integer.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The pattern is a simple array scan. Walk the indices once, test eligibility, compute abs(floor - passengerFloor), and keep the best. Eligibility: IDLE always passes. UP passes only if the passenger direction is UP and floor <= passengerFloor. DOWN passes only if the passenger direction is DOWN and floor >= passengerFloor. The common pitfall is sorting by distance first, or ignoring direction when the elevator is on the same floor. Another is using a <= comparison on distance, which breaks the smaller-index tie rule. Use strict less-than and the first index wins automatically. Watch overflow too. Floors are 32-bit signed ints, so the difference can exceed int range. Use a 64-bit type for the distance. That's O(n) time and O(1) space. If you freeze on the three-branch condition during the live OA, StealthCoder can hand you the clean version while staying invisible to the proctor.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Nearest Eligible Elevator 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. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Pinterest's OA.
Pinterest reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Nearest Eligible Elevator FAQ
How hard is Nearest Eligible Elevator really?+
Easy. It's one pass over the arrays with a conditional and a minimum tracker. The difficulty is reading the rules carefully, not the algorithm. Most failures come from the direction conditions or the tie-break, not from complexity.
What's the trick to getting this right?+
Write the eligibility check as its own condition before touching distance. Idle passes, up needs passenger up and elevator at or below, down needs passenger down and elevator at or above. Then compare distance with strict less-than so ties keep the lower index.
Why does Example 2 return index 2 and not 1?+
Elevator 1 is on floor 9 but moving down, and the passenger wants up. Direction mismatch makes it ineligible regardless of distance. Elevator 2 is idle on floor 9, so it qualifies at distance zero.
Do I need to worry about integer overflow?+
Yes. Floors are 32-bit signed integers, so subtracting a large positive from a large negative can overflow. Compute the absolute distance in a 64-bit type like long in Java or C++. Python handles it natively.
How do I prepare for this in 48 hours?+
Practice linear-scan-with-filter problems and tie-break handling. Write this one from scratch and run all four examples by hand. Spend extra time on edge cases: empty arrays, all ineligible, and equal distances from both sides of the passenger.