Pinterest phone screen, elevator dispatch question (passed)
Pinterest phone screen, elevator dispatch question (passed)
Had my Pinterest SWE phone screen last week, one coding question for the full hour. Just heard I'm moving to onsite, so dumping the problem here before I forget.
It was the call elevator question. You get a list of elevators, each with a current floor and a state (up, down, or idle). A passenger on some floor wants to go up or down and you return which elevator takes the call. Floors come in as strings btw, I almost forgot to parse them.
The eligibility rules are where I slowed down. Idle can take anyone. A moving elevator only counts if the passenger wants the same direction and is in its path. So one on 12 heading down can grab someone on 9 who wants down, but not someone on 14, and not someone on 9 going up. Among eligible ones pick the closest, ties go to the lower index, and if nobody qualifies you return a "none available" signal.
Code was one greedy pass tracking best distance and index. I pulled eligibility into a helper and the interviewer liked it since we walked cases just by calling it.
Last 10 min he asked what changes if requests arrive over time and elevators move a floor per second. I talked through advancing every elevator to the current time and reusing the same function, no code.
Not hard tbh, you just can't botch the direction checks.