Amazon Interview Question for Senior Software Development Engineers


Team: Echo
Country: United States
Interview Type: Phone Interview




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

Class ReservationSystem{
	HashTable <Long,Reservation> reservationTable = new HashTable<>();
	
	public void createReservation(long startTime,String firstName, String LastName,int partySize){
	Random random = new Random();
	long reservationID = random.getNextLong(10000);
	Person reserver = new Person();
	reserver.firstName = firstName;
	reserver.lastName = lastName;
	reservationTable.put(reservationID, new Reservation(reservationId,startTime,reserver,partySize);
	}
	public Reservation searchReservation(){}
	public void cancelReservation(){}
}
Class Reservation{
	long reservationID;
	long startTime;
	Person reserver;
	int partySize;
	public Reservation( long reservationID, long startTime, Person reserver, int partySize){
	this.startTime = startTime;
	this.reserver = reserver;
	this.partySize = partySize;
	}
	
}

Class Person
{
	String firstName;
	String lastName;
	String phoneNumber;
	String email;
}

- vfours July 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is the right data model. ReservationSystem forms part of app service level and the Reservation, Person, Guest etc. form the domain model.
If not explicity requested, the details of available table selection etc. are not required. If asked, they will form the part of the app service layer and not the model.

- Abel December 26, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Great. One reservation system which accepts several reservations initiated by respective person.

In addition to this, one basic thing that ReservationSystem class should have is 'TotalSeatCount'. If a requestor asks for a reservation for few people, the system will tell the requestor to wait accordingly. Also, there should be one method 'completeReservation()' which will be called once the user leaves the restaurant and 'notifyUser()' method will be auto-triggered to notify the next user in the waiting queue. Now, waiting queue logic can be developed independently.

- Psycho January 07, 2016 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

Reservation system is supposed to allow a user make reservation for a table in the restaurant for given time duration and given day. Reservation should be aware of the tables available in restaurants and tables already reserved. Tables can be of various size e.g., 4, 6, 8 etc.. and of various type e.g., conference, general etc.. Reservation request can contain the size and the type of the table. Cancellation requires to specify the reservation id.

class ReservationSystem
{
	List<Table> avilableTables=new List<Table>();
	List<Table> occupiedTables=new List<Table>();
	Dictionary<int, Reservation> reservations=new Dictionary<int, Reservation>();
	public int Reserve(int size, int type, int numPeople, DateTime dt, int duration, Customer c)
	{
	}
	public bool CancelReservation(int reservationId)
	{
	}			
}

class Table
{
	int size, type;
}

class Reservation
{
	int id;
	datetime dt;
	int duration;
	int numPeople;
}

class Customer
{
	string name;
	int age;
}

- spiderman August 29, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Person {
	String name;
	
	public person (String name) {
		this.name = name;
	}
	
	public String getName() {
		return this.name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
}

class Guest extends Person {
	String phone;
	String email;
	
	public guest (String name) {
		super.name = name;
	}
	public String getName() {
		return super.name;
	}
	
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getPhone() {
		return this.phone;
	}
	
	public void setEmail(String email) {
		this.email = email;
	}
	public String getEmail() {
		return this.email;
	}
	
	public void setName(String name) {
		super.name = name;
	}
}

class Reservation {
	/* I don't need these instance variables as I am string the information in the database. These would have been useful if I
	was storing this in a data structure like HashMap.*/
	
	// String tableId;
	// Person staff;
	// Guest guest;
	// Date reservationDate;
	// Time reservationTime;
	
	public makeReservation (String tableId, Person staffObj, Guest guestObj, Date date, Time time) {
		// Make a database connection.
		// Email and phone can be optional, so we won't use it as Primary key.
		// Make combination of guestName, date, time as Primary key.
		// Get all the properties of staffObj & guestObj and insert it.
		// Insert all the properties in the Reservation table.
	}
	
	public cancelReservation (String guestName, Date date, Time time) {
		// Delete from Reservation table based on guestName, date, time
	}
	
	public pullUpReservation (String guestName, Date date, Time time) {
		// Select from Reservation table based on guestName, date, time
	}
	
	public pullUpReservation (String guestName) {
		// Select from Reservation table based on guestName
	}
	
	public pullUpReservation (String guestName) {
		// Select from Reservation table based on guestName
	}
	public pullUpReservation (String guestName) {
		// Select from Reservation table based on guestName
	}
	
	public pullUpReservation (String Date date, Time time) {
		// Select from Reservation table based on date, time
	}
	
}

- Anonymous August 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Person {
	String name;
	
	public person (String name) {
		this.name = name;
	}
	
	public String getName() {
		return this.name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
}

class Guest extends Person {
	String phone;
	String email;
	
	public guest (String name) {
		super.name = name;
	}
	public String getName() {
		return super.name;
	}
	
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getPhone() {
		return this.phone;
	}
	
	public void setEmail(String email) {
		this.email = email;
	}
	public String getEmail() {
		return this.email;
	}
	
	public void setName(String name) {
		super.name = name;
	}
}

class Reservation {
	/* I don't need these instance variables as I am string the information in the database. These would have been useful if I
	was storing this in a data structure like HashMap.*/
	
	// String tableId;
	// Person staff;
	// Guest guest;
	// Date reservationDate;
	// Time reservationTime;
	
	public makeReservation (String tableId, Person staffObj, Guest guestObj, Date date, Time time) {
		// Make a database connection.
		// Email and phone can be optional, so we won't use it as Primary key.
		// Make combination of guestName, date, time as Primary key.
		// Get all the properties of staffObj & guestObj and insert it.
		// Insert all the properties in the Reservation table.
	}
	
	public cancelReservation (String guestName, Date date, Time time) {
		// Delete from Reservation table based on guestName, date, time
	}
	
	public pullUpReservation (String guestName, Date date, Time time) {
		// Select from Reservation table based on guestName, date, time
	}
	
	public pullUpReservation (String guestName) {
		// Select from Reservation table based on guestName
	}
	
	public pullUpReservation (String guestName) {
		// Select from Reservation table based on guestName
	}
	public pullUpReservation (String guestName) {
		// Select from Reservation table based on guestName
	}
	
	public pullUpReservation (String Date date, Time time) {
		// Select from Reservation table based on date, time
	}
	
}

- Using database to make data persistent August 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Person {
	String name;
	
	public person (String name) {
		this.name = name;
	}
	
	public String getName() {
		return this.name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
}

class Guest extends Person {
	String phone;
	String email;
	
	public guest (String name) {
		super.name = name;
	}
	public String getName() {
		return super.name;
	}
	
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getPhone() {
		return this.phone;
	}
	
	public void setEmail(String email) {
		this.email = email;
	}
	public String getEmail() {
		return this.email;
	}
	
	public void setName(String name) {
		super.name = name;
	}
}

class Reservation {
	/* I don't need these instance variables as I am string the information in the database. These would have been useful if I
	was storing this in a data structure like HashMap.*/
	
	// String tableId;
	// Person staff;
	// Guest guest;
	// Date reservationDate;
	// Time reservationTime;
	
	public makeReservation (String tableId, Person staffObj, Guest guestObj, Date date, Time time) {
		// Make a database connection.
		// Email and phone can be optional, so we won't use it as Primary key.
		// Make combination of guestName, date, time as Primary key.
		// Get all the properties of staffObj & guestObj and insert it.
		// Insert all the properties in the Reservation table.
	}
	
	public cancelReservation (String guestName, Date date, Time time) {
		// Delete from Reservation table based on guestName, date, time
	}
	
	public pullUpReservation (String guestName, Date date, Time time) {
		// Select from Reservation table based on guestName, date, time
	}
	
	public pullUpReservation (String guestName) {
		// Select from Reservation table based on guestName
	}
	
	public pullUpReservation (String guestName) {
		// Select from Reservation table based on guestName
	}
	public pullUpReservation (String guestName) {
		// Select from Reservation table based on guestName
	}
	
	public pullUpReservation (String Date date, Time time) {
		// Select from Reservation table based on date, time
	}
	
}

- Coder August 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This Person class can be extended to waiters, managers, guests etc.

Class Person {
	String firstName;
	String lastName;
	String contactNo;
}

Class Guest extends Person {
	String email;
	String address;

	// All the setter and getter methods
}

This class Table is required as there are various types of tables and with different seats. Also, the occupancy time of each table differs accordingly.

Class Table {
	long tableID;		// table identifier
	int seatingCapacity;	// Max number of guests it can accomodate
	int category;		// It's not really important but various categories like family, dinner, couple is required
	bool inOccupied;	// whether this table is currently occupied or not.
	bool occupiedHalfHours[48];	
	// this variable is trickier. We divided our 24 hours into 48 halves. Whenever we book 
	// this table set true is occupied for that specified half hour. This variable is mainly used 
	// to checking availability of this table during any specific time.

	public Table() { // initialize all the variables }
	public bool isReserved (long start, long end) { // checks whether this table is reserved for this time window or not}
	public bool isFitToRequest(int totalNum, long start, long end) {}
}

This is the booking or reservation class and will be assigned to the requester.

class Reservation {
	long bookingID;		// reservation ID
	long startTime;			// when starts
	long endTime;			// when ends
	Guest askedPerson;	// who asks for this reservation
	int totalPersonCount;	// total member count

	// This details below will be filled by the Restaurant system
	bool isCancelled;
	bool isWaiting;
	ArrayList<Table> allocatedTables;	// which tables has been allocated to this reservation
	// Main reason for this is that, totalPersonCount may be greater than the seats available for
	// a particular table, in that case we need to allocate another table for same booking.
	
	private bool canFitIntoTable(Table t) {}
	public void notifyUserWithDetails()	// User will be informed
	// This function will be triggered if booking is successful, can not be made or in waiting.
}

Class ReservationSystem {
	int numberOfTables;	// Total numbers of table can be accomodate in this restaurant
	long openingTime;		// Restaurant opening time
	long closeTime;		// Closing time, all the requests should be performed in between these two times
	ArrayList<Table> tables;	// Holds all the table information, used to query the table inforamtion
	ArrayList<Reservation> waitingList;	// All the reservations currently in waiting state
	HashMap<Integer, Reservation> reservationMap;	// searches any reservation query with booking/reservation ID.

	// This variable is used to reduce the time for the query like this : "Give all the free tables"
	// at any specific time. We can extract the same information using 'tables' variable
	// as it holds the free slot times also. But in some heavy traffic restaurant, which accepts
	// pre-booking, looping all the tables and extracting the availability will be a tedious one. 
	// So, again 
	HashSet<Tables> freeSlots[48];

	public ReservationSystem() {}
	public boolean cancelReservation(long ID);
	public void contactCustomer(Reservation rID);
	public boolean closeReservation(long ID);
	public Reservation searchReservation(long ID);
	public int checkAvailablity(int noOfperson, long start, long end, ArrayList<Table> tables, ArrayList<long> alternateTimes);
}

Class Restaurant {
int restaurantID;        // restaurant ID
long timeOfStarting;  // opening time
long timeOfClosing;  // closing time
int totalSeats;    // total seats that this reservation has
ReservationSystem system;
void iniitalizeReservationSystem(long openTime, long closingTime, int seats);

// All the basic query interface to the reservation system
}

- Psycho January 10, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;
import java.util.HashMap;

public class ReservationSystem {
HashMap<Long, Reservation> map;
//From 10am to 9pm, each hour have a list of tables
ArrayList<Table>[] tables;
// two hour between each reservation for one table
int MAX_TIME = 2;

public Reservation getResrvation(Custmer custmer) {
return map.get(custmer);
}

public void makeReservation(Custmer custmer, int startTime, int size) {
Table newTable = findTable(startTime, size);
if(newTable != null) {
Reservation newReservation = new Reservation(newTable,custmer,startTime);
long custmerId = custmer.getId();
map.put(custmerId, newReservation);
removeTable(startTime, newTable);
} else {
System.out.println("Can not find table !!");
}
}

public void cancelReservation(Custmer custmer) {
Reservation newReservation = map.remove(custmer.getId());
addTable(newReservation.getTime(),newReservation.getTable());
}

public Table findTable(int startTime, int size) {
ArrayList<Table> tableList= tables[startTime - 10];
for(Table t : tableList) {
if(t.getSize() >= size) {
return t;
}
}
return null;
}

public void removeTable(int startTime, Table table) {
int index = startTime-10;
int period = MAX_TIME;
while(index+period-1<tables.length && period > 0) {
tables[index+period].remove(table);
period--;
}
}

public void addTable(int startTime, Table table) {
int index = startTime-10;
int period = MAX_TIME;
while(index+period-1<tables.length && period > 0) {
tables[index+period].add(table);
period--;
}
}

public Reservation getReservation(Long custerId) { return map.get(custerId);}
}

class Reservation {
protected long ReservationId;
protected Table table;
protected Custmer custmer;
protected int startTime;

public Reservation(Table table, Custmer custmer, int startTime) {
this.table = table;
this.custmer = custmer;
this.startTime = startTime;
}

public Table getTable() { return table;}
public int getTime() { return startTime;}
}


class Table {
protected long tableId;
protected int size;

public Table(int size) { this.size = size;}
public int getSize() { return size; }
}

class Custmer {
protected String name;
protected long custmerId;
protected String mail;
protected String phone;

public long getId() { return custmerId;}
}

- Xiang January 25, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here there is one more case to handle. For example if we tie the reservation of customer to a particular table, then it may not be an optimal solution. The availability of the tables are not strictly time bound. That means if the customer has reservation at 9pm for table-1, and table-1 got not vacated by the earlier customer, then customer has to wait till it get vacated. Here we are not considering the cases, where other tables got vacated and available for seating.

It would be ideal, if we could maintain a queue of the customers reservations and available tables as separate list, then allocation of the table to the customer will be as per the arrival of the customers.

Here the advantage is the restaurant can decide on how to handle the customer reservation queue. Here the design should take care of table size during allocation.

- Kumar_m January 30, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Merhaba,


Great post. Well though out. This piece reminds me when I was starting out Amazon Interview Question for Senior Software Development Engineers after graduating from college.

This is Erin from Amazon Web Services. I took a look at the support case you provided, I see that one of our agents was able to assist and resolve the case.

Please let us know if there is anything else we can help with!



Awesome! Thanks for putting this all in one place. Very useful!

Kind Regards,
Radhey

- Radhey June 01, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Thanks but where is the main method?

Best,
Addy

- Anonymous April 17, 2020 | 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