Amazon Interview Question for SDE1s


Team: Search
Country: United States
Interview Type: In-Person




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

Here is my attempt at OO design. Main classes are: Elevator, Request and Person. Request is an abstract class and the classes that implement/extend it are upRequest, downRequest, emergencyExitRequest and whatever you want. ElevatorSystem is the driver class. My design closely resemble the strategy pattern as I am choosing the serviceRequest implementation at runtime.

Haven't thought about an algorithm to service requests efficiently. Will do it later.

Class Elevator{
     Private:

          Person *in; //List of persons inside the elevator
          int destinationFloor;  //Floor to which the elevator is going;
          int currentFloor;
          enum {UP, DOWN, STATIONARY} stateOfMotion;
      
     Public:
          makeRequest(Request *){
               //Handle various types of requests and reset destinationFloor and stateOfMotion based on the  request by calling serviceRequest. Hook for implementing efficient algorithm for servicing requests of different priorities.

          }

          enterElevator(Person){
               //Open door, allow to enter a person and add Person to in
          }

          exitElevator(Person){
               //Open door, allow to exit a person and remove Person from in
          } 
} 

Class Person{
     Private:
          int floor;
     
     Public:
          requestElevator(Request *){
               //Make a request to the elevator
          }

          setFloor(int floor){
               //set floor
          }
     
}


Class Request{
     Private:
          int priority;
	  timeStamp;
     
     Public{
          virtual void serviceRequest(int &destination){};
          void setPriority(){
          }
}


Class UpRequest : public Request{
//Implement go up request, this will be made typically from outside the elevator
}


Class DownRequest : public Request{
//Implement go down request, this will be made typically from outside the elevator
}

Class EmergencyExitRequest : public Request{
//Implement emergency exit request, this will be made typically from inside the elevator
}

Class DestinationFloorRequest : public Request{
//Implement go to specific floor request, this will be made typically from inside the elevator 
}


Class ElevatorSystem{
//Main class for the system
//This will be responsible for creating an Elevators and Persons
}

Sorry for taking liberties with C++ syntax.

- Epic_coder June 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I didn't look at the whole design, just a few things that popped out to me:
1. I would take out the "STATIONARY" from the enum and have a isMoving boolean. The reason is that the elevator may stop to let people in and out, but it still has a direction. In your design, you cannot represent this.
2. I wouldn't use a Person class nor keep track of the persons. Think about a real elevator. Does it know who or even how many persons are in it?

- Yoni June 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

You need to keep track of people inside it because elevators have a max limit of number of people it can move safely.

- Epic_coder June 16, 2013 | Flag


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