Start learning free

LESSON 337 OF 343 · OBJECT-ORIENTED DESIGN - INTERVIEWS

Hotel Reservations

Practice O(k)

This is a design round. You write the classes, and the interviewer checks that they behave.

Here you build the booking system of a small hotel. It sells three types of room: "single", "double" and "suite". The rooms have no numbers. A guest books a type of room for some nights.

Implement two classes:

  • Reservation(guest, roomType, checkIn, checkOut) keeps one booking. Its fields hold the four values.
  • Hotel(singles, doubles, suites) starts with that many rooms of each type, all free.
  • reserve(guest, roomType, checkIn, checkOut) books one room for a stay. It returns a confirmation number, or 0.
  • cancel(number) cancels a reservation. It returns true if it did.
  • freeRooms(roomType, night) returns how many rooms of that type are free on that night.
  • occupied(night) returns how many rooms are taken on that night, of all types.
  • guest(number) returns the guest's name on a reservation.

Nights are numbered from 1 to 365. A stay from checkIn to checkOut covers the nights checkIn to checkOut - 1. So a stay from 3 to 5 covers nights 3 and 4. The guest leaves on day 5, and a new guest can have the room that night.

Each reservation holds one room of its type on every night of the stay.

A reservation is refused when, on some night of the stay, every room of that type is taken. Then reserve returns 0 and changes nothing.

The first reservation that succeeds gets confirmation number 1. The next one gets 2, and so on. A refused call gets no number. A number is never given twice, even after a cancel.

cancel frees the room on every night of that stay. It returns false when no reservation has that number. It also returns false when the reservation was already cancelled.

guest returns "none" when no reservation has that number, or when it was cancelled.

The same guest may hold many reservations, even for the same nights.

The calls run in order on one new Hotel. A call to reserve writes the number it returns. A call to cancel writes True or False. A call to freeRooms or occupied writes the number. A call to guest writes the name.

 

Example 1:

Input:
Hotel(2, 1, 0)
1. reserve("Ana", "single", 3, 5)
2. reserve("Ben", "single", 4, 6)
3. freeRooms("single", 4)
4. reserve("Cy", "single", 4, 5)
5. reserve("Cy", "double", 4, 5)
6. occupied(4)
7. freeRooms("single", 5)
8. guest(2)
Output:
1
2
0
0
3
3
1
Ben
Explanation:
Ana's stay covers nights 3 and 4. Ben's stay covers nights 4 and 5.
So both singles are taken on night 4.
Call 4 is refused, because no single is free on night 4.
A double is still free, so call 5 works. It gets number 3.
Three rooms are taken on night 4.
On night 5, only Ben's single is taken.
Call 8 asks who holds number 2. It is Ben.

Example 2:

Input:
Hotel(1, 0, 1)
1. reserve("Dee", "suite", 10, 13)
2. reserve("Eve", "suite", 12, 14)
3. cancel(1)
4. cancel(1)
5. guest(1)
6. reserve("Eve", "suite", 12, 14)
7. freeRooms("suite", 11)
8. cancel(5)
9. guest(2)
Output:
1
0
True
False
none
2
1
False
Eve
Explanation:
Dee's suite covers nights 10, 11 and 12.
Eve wants nights 12 and 13. Night 12 has no free suite, so call 2 returns 0.
Call 3 cancels Dee's stay. The same cancel again returns False.
A cancelled number has no guest, so call 5 writes none.
Night 12 is free again, and Eve gets number 2.
The suite is free on night 11 now.
No reservation has number 5.

Example 3:

Input:
Hotel(1, 1, 0)
1. reserve("Fay", "double", 2, 4)
2. reserve("Gus", "double", 1, 3)
3. freeRooms("double", 1)
4. reserve("Gus", "double", 4, 6)
5. occupied(3)
6. occupied(4)
7. reserve("Hal", "suite", 1, 2)
8. reserve("Fay", "single", 2, 4)
9. occupied(2)
Output:
1
0
1
2
1
1
0
3
2
Explanation:
Fay's double covers nights 2 and 3.
Gus asks for nights 1 and 2. Night 2 is full, so call 2 returns 0.
Nothing was booked for Gus. So night 1 still has a free double.
Gus arrives on day 4, the day Fay leaves. That works.
Nights 3 and 4 each have one room taken.
The hotel has no suites, so call 7 returns 0.
A guest may hold two rooms. Call 8 gives Fay a single for the same nights.
So Fay holds two rooms on night 2.

 

Constraints:

  • singles, doubles and suites are each from 0 to 1,000.
  • roomType is always "single", "double" or "suite".
  • A name has 1 to 10 English letters.
  • A name starts with a capital letter, and its other letters are lowercase.
  • 1 <= checkIn < checkOut <= 366.
  • A stay has at most 30 nights.
  • Each night is from 1 to 365.
  • Each number is from 1 to 100,000.
  • At most 100,000 calls follow the constructor.

Note:

A call to reserve or cancel should take O(k) time, for a stay of k nights. A call to freeRooms, occupied or guest should run in O(1) time. Keep a count of the rooms taken on each night, per type and for all types together.

STEP 1 OF 35
    Ctrl + Enter to check