Amazon Interview Question
Country: United States
Elevators can be programmed so that they can only serve certain floors or floor ranges
So,using Divide and conquer method optimizes your problem by reducing the number of floors an elevator serves based on elevator location like (Higher the floor and less the number of floors,elevator serves)
You can also give priority based on the busy time
I would start my answer by trying to lay out some rules:
1) Once a passenger gets on the elevator, the elevator must deliver him to any floor in the direction he indicated while waiting for the elevator.
2) The elevator system must serve the extremes, so if an elevator is going down, and people are waiting on the bottom, it can't turn around to get more passengers on middle floors unless there's another elevator working toward the bottom.
The interviewer might challenge both rules. For rule #1, you would probably make an exception if you have just one passenger. For rule #2, an unethical elevator operator might decide overall throughput is worth temporarily standing people for a while (especially if they can take the stairs).
Once an elevator is empty, it needs to decide whether to work up or down, based on the other elevator's current travel plans. A greedy plan has you going to the nearest extreme, i.e. if the elevator is close to the top, go pick up the topmost passenger. But you need to account for other elevators maybe already being en route to that passenger (especially empty elevators that are already above you).
I think you have create a simple algorithm which will calculate the difference in current floor and called floor as well as the difference between the called floor and resultant floor.
Ex. if the lift is at 2nd floor and the next floor is 10th. If someone calls from 1st, it must go to first rather than moving up to the 10th.
That's what i think.
I can be wrong. What say?
I think that approach will not work in normal way. Let's consider a case.
Lift is going upwards, currently at Floor 2.
Some one calls at Floor 1 then it goes there instead of Floor 10
and take person from floor 1 and then start moving to floor 10 again someone call at floor 1.
By doing this lift will be in infinite loop in floor 1 and floor 2.
Use Skiplist. (See wikipedia)
This is similar to Subway system, where there will be certain express trains and certain local trains. Similarly, there will be certain elevators, servicing major floors (where no. of people getting off is more) and other elevators will serve local floors. Reducing average wait time for all people to optimal
You have to keep in mind about the number of people going to use the elevator and number elevators. Once you have those values, divide the number of buttons(floors it will go) you are going to put in each elevator into ranges rather than all in in each elevator. So now your elevators will have range of floors it will travel. This reduce overall waiting time.
- Koteshwar Reddy Busi March 03, 2013