Amazon Interview Question
SDE-2sCountry: United States
Interview Type: In-Person
then problem here is that we can't have the 40 buttons or 60 buttons.
we can have indexed approach here to have button like
0, 10, 20, 30, 40 and corresponding we can 1,2,3,4,5,6,78,9,10 buttons.
On the same line we can have a solution with floors marked as A1,A2, A3 , B1, B2, B3. After this we can have indexed button A , B ,C instead of 10,20, 30.
Let's get into little math here!!
Let's say there N number of lifts are there in building.
Each floor is expected to have at-least M Request panels/DisplayUnits(call them IOPanel).
Whole building can have total of P number of IOPanels.
IOPanel:
-has an Identity.
-handles user input/output operations.
-creates a ElevatorRequest on behalf of USER.
-places request to MainRequestHandler.
Request:
-has an Identity
-knows sourceFloor
-knows targetFloor
-knows RequestOriginator(IOPanel)
MainRequestHandler:
-maintains RequestPool
-can Add a request to it's pool
-can delete a request to it's pool.
-For Each Request in RequestPool,
it Uses a Scheduler to assign the request to a ElevatorRequestHandler.
remove request from pool.
-**Uses a schedular
Scheduler:
-uses SchedulerStrategy
-can observe the state of ElevatorRequestHandler(s) to come up with response.
SchedulerStrategy:
- Abstract Strategy can have concrete strategy classes inheriting from this.
ElevatorRequestHandler:
- processes the Request from it's pool.
- can control the Elevator accordingly. {start,stop,shutdown,set direction etc}
Elevator:
-knows it's current position.
-can move up based on command given by it's controller.
-can move down based on command given by it's controller.
-has Door (can open door/close door)
SchedulerStrategy => can be Strategy Pattern
MainRequestHandler and ElevatorRequestHandler(s) => can be Command Pattern
Now worry about scheduling for a large building. i.e. discuss a particular strategy and see how your strategy can fit in StrategyPattern.
The data structures involved should be some kind of circular priority queue, priority being lower floors and upper floors alternatively according to when the elevator is going up or coming down.
The functions would be like :
Start
- manually start the elevator
- allows pressing of button from outside the elevator to make a call
Stop
- manually stop the elevator
while the elevator is in started mode following functions would be applicable
while door is open at some floor
- keep count on the number of people entering and their weights
- if weight rises above the limit - generate an alert and stop until some people get out
- otherwise let people in
- for people inside allow them to enter the floor numbers of where they want to stop
- all these will be stored in the priority queue.
- when no one is coming in for a few minutes close the door and move
the elevator can move either up or down according the value pressed first when the queue was empty
while going up/down
- stop on each floor in the queue which comes in the way to the floor having the highest value/lowest.
- also stop at floors where the button for call has been pressed for going up/down respectively.
- at each stop open the doors and follow the above function
- when the floor with highest/lowest value is reached, check if the list is empty or not.
- if empty close the door and wait for some input at some floor from outside.
other input values can be like
- alarm button for emergencies
- fan on/off etc
I was asked the same question in one of the interviews and off-course couldn't do well in the interview.
- Algorithmy October 28, 2014There is 2 questions related to elevators that get asked
1.) Object oriented design for elevator system
2.) Scheduling of the elevator which is another complicated subject.
The following below should help with 1.).
Please note that the below content is from one of the papers on internet. unfortunately, i cannot link the web document here. search in google for "elevator system object oriented design" and you should find more details on the problem. It is domain cmu.edu.
---------------------------
Car: The car is being controlled to move up and down (in different speeds), to makestops at floors when necessary.
Button: The ElevatorControl class also controls the button class, which further generalizes two subclasses CarCallButton and HallCallButton. The control object
communicates with the Button objects, get the information whether a button is pressedand in turn controls the illumination of Button lights.
Indicator: There are two kinds of indicators in the system, the CarPositionIndicator and the CarDirectionIndicator (i.e. the CarLantern). The indicators are controlled to show the information about the current position and moving direction of the car.
Safety: Whenever an emergency happens according to the definition of emergency brake trigger in the requirement documentation, the ElevatorControl commands the Safety
---------------------------