Opentable Interview Question for Backend Developers


Country: United States
Interview Type: Phone Interview




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

import java.time.Duration;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//Your goal is to write business logic for a very simple Restaurant booking system
//You are encouraged to refactor exisiting code, create other classes, write helper methods etc
//You also need to make sure that the implementation works correctly

class Reservation {
	public String name;
	public int partySize;
	public LocalTime startTime;
	public LocalTime endTime;
}

class Table {
	public int tableNumber;
	public int maxPartySize;
}

public class Restaurant {

	public List<Table> tables;
	public LocalTime openTime;
	public LocalTime closeTime;
	public Map<Integer, Duration> reservationDurationsPerPartySize;
	public Map<Reservation, Table> reservations;
	
	public Restaurant(LocalTime openTime, LocalTime closeTime) {
		this.openTime = openTime;
		this.closeTime = closeTime;
		tables= new ArrayList<Table>();
		reservationDurationsPerPartySize = new HashMap<Integer, Duration>();
		reservations = new HashMap<Reservation, Table>();
	}
	
	public Reservation buildReservation(String name,
			int partySize,
			LocalTime startTime) {
		Reservation r = new Reservation();
		r.name=name;
		r.partySize = partySize;
		r.startTime = startTime;
		if (reservationDurationsPerPartySize.containsKey(r.partySize)) {
			r.endTime = startTime.plus(reservationDurationsPerPartySize.get(r.partySize));
		} else {
			r.endTime = LocalTime.of(23,59);
		}
		return r;
	}

	// Returns a Table if Reservation could be booked, null otherwise
	// Booking rules:
	// 1) Reservation could be made only when the Restaurant is open.
	// 2) Only one Reservation can be seated a Table at any time.
	// 3) Reservation can be seated only at a Table of the same or a bigger
	// size.
	// 4) Reservation should stay on the same Table for the whole Duration.
	// 5) Reservation Duration is determined by PartySize.

	public Table bookReservation(Reservation reservation) {
		if (reservation.startTime.isAfter(openTime) && reservation.startTime.isBefore(closeTime)) {
			for (Table t : tables) {
				if (t.maxPartySize<reservation.partySize) {
					continue;
				}
				boolean isOk = true; 
				for (Reservation r : reservations.keySet()) {
					Table t2 = reservations.get(r);
					if (t2==t) {
						if (r.startTime.isAfter(reservation.endTime)
								|| r.endTime.isBefore(reservation.startTime)) {
						} else {
							isOk = false;
						}
					}
				}
				if (isOk) {
					reservations.put(reservation, t);
					return t;
				}
			}
			return null;
		} else {
			return null;
		}
	}
}

- jason April 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Generate endTime for new reservations.
2. Try to find a table that can 1) fit the party, and 2) no reservations in that period.

Further improvement:
Each table has its own reservation list, or a segmentation tree.

import java.time.Duration;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//Your goal is to write business logic for a very simple Restaurant booking system
//You are encouraged to refactor exisiting code, create other classes, write helper methods etc
//You also need to make sure that the implementation works correctly

class Reservation {
	public String name;
	public int partySize;
	public LocalTime startTime;
	public LocalTime endTime;
}

class Table {
	public int tableNumber;
	public int maxPartySize;
}

public class Restaurant {

	public List<Table> tables;
	public LocalTime openTime;
	public LocalTime closeTime;
	public Map<Integer, Duration> reservationDurationsPerPartySize;
	public Map<Reservation, Table> reservations;
	
	public Restaurant(LocalTime openTime, LocalTime closeTime) {
		this.openTime = openTime;
		this.closeTime = closeTime;
		tables= new ArrayList<Table>();
		reservationDurationsPerPartySize = new HashMap<Integer, Duration>();
		reservations = new HashMap<Reservation, Table>();
	}
	
	public Reservation buildReservation(String name,
			int partySize,
			LocalTime startTime) {
		Reservation r = new Reservation();
		r.name=name;
		r.partySize = partySize;
		r.startTime = startTime;
		if (reservationDurationsPerPartySize.containsKey(r.partySize)) {
			r.endTime = startTime.plus(reservationDurationsPerPartySize.get(r.partySize));
		} else {
			r.endTime = LocalTime.of(23,59);
		}
		return r;
	}

	// Returns a Table if Reservation could be booked, null otherwise
	// Booking rules:
	// 1) Reservation could be made only when the Restaurant is open.
	// 2) Only one Reservation can be seated a Table at any time.
	// 3) Reservation can be seated only at a Table of the same or a bigger
	// size.
	// 4) Reservation should stay on the same Table for the whole Duration.
	// 5) Reservation Duration is determined by PartySize.

	public Table bookReservation(Reservation reservation) {
		if (reservation.startTime.isAfter(openTime) && reservation.startTime.isBefore(closeTime)) {
			for (Table t : tables) {
				if (t.maxPartySize<reservation.partySize) {
					continue;
				}
				boolean isOk = true; 
				for (Reservation r : reservations.keySet()) {
					Table t2 = reservations.get(r);
					if (t2==t) {
						if (r.startTime.isAfter(reservation.endTime)
								|| r.endTime.isBefore(reservation.startTime)) {
						} else {
							isOk = false;
						}
					}
				}
				if (isOk) {
					reservations.put(reservation, t);
					return t;
				}
			}
			return null;
		} else {
			return null;
		}
	}
}

- jason April 07, 2017 | 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