Amazon Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
2
of 2 vote

@Anon- I tried solving this problem by command pattern but it is not fitting in it.
I 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)

- Nishank Rajvanshi September 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think this design is good, but it doesn't take concurrency or the time it takes an elevator to move to a certain floor into account. Like your moveUp() method, it changes the movingDir but never changes it back to Stationary when it arrives.

- Guy February 11, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

we 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.

- superstart July 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Any idea about the design patterns? Is this stressed upon? Can we use command pattern

- Anon September 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do we need to use Threads? Any suggestion?

- Anonymous November 03, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

We can use Observer Pattern.

- Unknown March 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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 ?

- Damaji kalunge January 01, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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.

- Damaji kalunge January 01, 2016 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More