Prepin
Log in

G

Create an algorithm for an elevator that picks up passengers.

Approach / Explanation

In this question, the interviewer aims to evaluate your problem-solving skills, your knowledge of algorithms, and your system design capabilities. You are required to develop an algorithm that an elevator system could use to effectively pick up passengers. The question is open-ended, allowing you to explore various aspects such as efficiency, fairness, and simplicity. You should clarify how your algorithm will balance these different considerations. You might begin by outlining your algorithmic approach, then move on to explain how it will manage requests and operate in various scenarios. The focus is not on finding a "correct" answer but rather on illustrating the thought process that led to the creation of the algorithm and how it can be adapted to different circumstances. Suggestions: Begin by explaining the fundamental operation of an elevator. Outline your vision for efficiently picking up passengers. Take into account factors such as the number of floors, the number of passengers, and the current direction of the elevator. Address edge cases, including what occurs if the elevator reaches capacity or if all passengers are headed in the same direction.

Suggested Answer

The proposed algorithm is called the “Scan” algorithm, which operates on the principle of moving in one direction until a request is received. To design an efficient elevator system for picking up passengers, it should be based on the elevator's current status. For example, if the elevator is currently on floor X and ascending, and a request is made from floor Y (where Y > X) for an upward ride, the elevator should accept this request and proceed to floor Y. If a request is made for a downward trip, the elevator will first complete all upward requests before addressing any downward ones. This approach allows the elevator to prioritize requests according to its current direction and proximity, leading to more efficient pick-ups and reduced waiting times. Edge Cases: In situations where the elevator is at full capacity or all passengers are traveling in the same direction, the elevator may temporarily adjust its priorities or revert to a single-direction mode until the number of passengers decreases or requests become more varied.

Alternative Answer

I propose a solution utilizing a "Priority Queue." This would involve two separate Priority Queues—one for requests to go up and another for down requests. When a guest submits a request, it would be added to the appropriate queue according to the direction, prioritizing by the floor number. The elevator will move in one direction, addressing all requests in that direction before reversing. For example, while ascending, it will continue to pick up passengers from higher priority requests (those on higher floors) until there are no more upward requests. After that, it will change direction and begin to fulfill the down requests. In cases where the elevator reaches full capacity, new requests should still be included in the queue, prioritized for service once space becomes available. If all passengers are traveling in the same direction, this method will efficiently manage their transport based on their respective floors.

Question Details

Difficulty & Category

Medium
analytical

Companies

ADP
Google

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.