Start learning free

LESSON 336 OF 343 · OBJECT-ORIENTED DESIGN - INTERVIEWS

Movie Ticket Booking

Practice O(k)

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

Here you build the seat booking for one show in a cinema hall. The hall has rows of seats. Every row has the same number of seats. Rows and seats are numbered from 1.

Implement two classes:

  • Booking(row, first, count) keeps one booking: its row, its first seat, and how many seats it has.
  • Cinema(rows, seats_per_row) makes a hall with rows rows of seats_per_row seats. All seats start free.
  • book(row, first, count) books count seats side by side in row, starting at seat first. It returns the booking's number, or 0.
  • cancel(number) cancels that booking. It returns True if it did.
  • is_free(row, seat) returns True if that seat is free.
  • free_in_row(row) returns how many seats of row are free.

A booking takes the seats first, first + 1 and so on, up to first + count - 1.

Sometimes book can't book. Then it returns 0 and changes nothing. This happens in three cases. The row isn't in the hall. A seat falls outside the row. One of the seats is already taken.

Booking numbers start at 1. Each booking that works gets the next number. A refused call doesn't use up a number. A number is never given out twice, even after a cancel.

A call to cancel frees the seats of that booking. Later calls can book them again. It returns False when no booking has that number. It also returns False when that booking was already cancelled. Then nothing changes.

The cinema keeps each booking as a Booking. That way cancel knows which seats to free.

The calls run in order on one new Cinema. A call to book writes the number. A call to cancel or is_free writes True or False. A call to free_in_row writes the number.

 

Example 1:

Input:
Cinema(3, 5)
1. book(1, 2, 3)
2. is_free(1, 3)
3. is_free(1, 5)
4. free_in_row(1)
5. book(1, 4, 2)
6. book(1, 5, 1)
7. book(2, 1, 5)
8. free_in_row(2)
9. free_in_row(3)
Output:
1
False
True
2
0
2
3
0
5
Explanation:
Booking 1 takes seats 2, 3 and 4 of row 1.
So seat 3 is taken, and seat 5 is free.
Row 1 has 2 free seats left: seats 1 and 5.
Call 5 fails, because seat 4 is taken.
Call 6 books seat 5 alone. It gets number 2.
Call 7 books all 5 seats of row 2.
Nobody booked in row 3.

Example 2:

Input:
Cinema(2, 4)
1. book(3, 1, 2)
2. book(1, 4, 2)
3. book(1, 5, 1)
4. book(1, 3, 2)
5. book(2, 1, 4)
6. free_in_row(1)
7. is_free(1, 4)
Output:
0
0
0
1
2
2
False
Explanation:
The hall has 2 rows of 4 seats.
Call 1 fails, because there is no row 3.
Call 2 fails, because row 1 has no seat 5.
Call 3 fails for the same reason.
Call 4 takes seats 3 and 4, the last two of row 1.
The refused calls used no number. So call 4 gets number 1.
Call 5 books all of row 2.
Row 1 still has seats 1 and 2 free. Seat 4 is taken.

Example 3:

Input:
Cinema(1, 6)
1. book(1, 1, 3)
2. book(1, 4, 2)
3. cancel(1)
4. is_free(1, 2)
5. cancel(1)
6. cancel(5)
7. book(1, 2, 3)
8. book(1, 1, 3)
9. free_in_row(1)
Output:
1
2
True
True
False
False
0
3
1
Explanation:
Booking 1 takes seats 1 to 3. Booking 2 takes seats 4 and 5.
Call 3 cancels booking 1, so seats 1 to 3 are free again.
Call 5 fails, because booking 1 is already cancelled.
Call 6 fails, because no booking has number 5.
Call 7 fails, because seat 4 still belongs to booking 2.
Call 8 gets number 3. A number is never given out twice.
Now only seat 6 is free.

 

Constraints:

  • rows and seats_per_row are whole numbers from 1 to 300.
  • In book, row and first are from 1 to 1,000.
  • count is from 1 to 10.
  • In cancel, number is from 0 to 100,000.
  • In is_free, the seat is always inside the hall.
  • In free_in_row, the row is always inside the hall.
  • At most 100,000 calls follow the constructor.

Note:

Each call to book or cancel should take O(count) time. Each call to is_free or free_in_row should take O(1). Looking through every booking on each call would take O(n) time, for n bookings.

STEP 1 OF 27
    Ctrl + Enter to check