Amazon Interview Question
Software Engineer / Developerswe can do this thing. some things we need to check before starting this problem.
see one thing is there:
at every level there will be one variable which will tell us the number of that floor at which the elevator is present currently.
after that the elevator will either be moving in the upward direction or the downward direction. if it is moving in the upward direction then it will not handle any request from the client if the request is for going down and vice versa.
I think its better to use command design pattern to process the request and to mange the state of an elevator use state Design Pattern.
--- The problem you have mentioned while using the command design pattern is
handling the state . You need single controller.
You can treat the concreateCommand as controller where you can queue the request and
and process .
What do you say ?
The best design to this problem is to use the command design pattern for processing the request and for handling the states of elevator use the state design pattern.
The problem you are mentioned while using the state design pattern is to know the current state.
You can treat the ConcreteCommand as the as controller where you keep the current state and queue the request and process.
The MVC helps here but to add new command and state . Need to modify the code at many places , thus it will become fragile.
@Anon- I tried solving this problem by command pattern but it is not fitting in it.
- Nishank Rajvanshi September 20, 2011I created two command objects - UpCommand and DownCommand and implemented the execute() method.I set the command objects in the invoker class using setCommand(Comand command) method and then called the work methods(metods which call execute method on the command objects).The problem whcih i faced was basically i am duplicating the logic of finding the correct elevator using command pattern, instead we can have a single controller which will control the elevators.
I think it is a perfect example of MVC pattern.Following is the first dig at it.
1)If elevator is going up , it will not process the down request and vice versa
2)If the request has been processed , then there should be a mechanism to garbage collect that request.
3)The requests should be stored in a queue.(This is the most challenging part of the problem and it is a potential thesis subject i think)
4)The elevator which is closest to current floor should process the request provided it is going in the same direction.
5)If all the elevators are stationary and at the same floor , then a random elevator should process the request.
Following are the classes;
1) ELEVATOR - Model Class
I will store two state variables a)String movingDir b)int FloorNum.
movingDir can have three values "UP", "DOWN" and "STATIONARY".People can also use ENUM , but i am not very comfortable in using enums.
Following will be the methods:
1) void moveUp(), set the movingDir to UP
2) void moveDown(), set the movingDir to DOWN
3) boolean isMoving() if movingDir =="STATIONARY" then return false.
4 String movingDir() return movingDir
5)void halt() movingDir = "STATIONARY"
2)CONTROLLER - Controller class
Following will be the methods;
1)init()- Initialize the Elevator system.This method will return an List of elevators with movingDir = "STATIONARY" and floorNum = 0. Basically its an initialize method and can be moved out of controller, because it will be called only once.its an admin kind of method.
2)Elevator upButtonPressed(int floorNum) - Method which is taking the current floor number.a
This method should iterate on the List and fetch all the elevators which are moving up and whose floor Numbers are less than passed Argument. We should store all these elevators in a priority queue where the key in priority queue will be floorNum - floorNumbers of the fetched elevators. We can easily fetch the elevator from a priority queue with the least priority.
2) Elevator downButtonPressed(int floorNum)- Same logic as above but in opposite order.
3)Other utility methods.
Following are the loose ends in the above design.
1) How is the initialization of elevators will be done and how that information is passed to controller initially in case we are not calling init method in Controller.
2)When will be the state of Elevator will be "STATIONARY".means how this logic will be handled.
3)How is concurrency handled.( A very big design consideration)