Start learning free

LESSON 326 OF 333 · OBJECT-ORIENTED DESIGN - INTERVIEWS

Movie Ticket Booking in Go

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:

  • NewBooking(row, first, count) keeps one booking: its row, its first seat, and how many seats it has.
  • NewCinema(rows, seatsPerRow) makes a hall with rows rows of seatsPerRow 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.
  • isFree(row, seat) returns true if that seat is free.
  • freeInRow(row) returns how many seats of row are free.

Go has no classes. Booking and Cinema are structs, made by NewBooking and NewCinema. The cinema's methods have a pointer receiver, *Cinema, because they change it.

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 isFree writes True or False. A call to freeInRow writes the number.

 

Example 1:

Input:
NewCinema(3, 5)
1. book(1, 2, 3)
2. isFree(1, 3)
3. isFree(1, 5)
4. freeInRow(1)
5. book(1, 4, 2)
6. book(1, 5, 1)
7. book(2, 1, 5)
8. freeInRow(2)
9. freeInRow(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:
NewCinema(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. freeInRow(1)
7. isFree(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:
NewCinema(1, 6)
1. book(1, 1, 3)
2. book(1, 4, 2)
3. cancel(1)
4. isFree(1, 2)
5. cancel(1)
6. cancel(5)
7. book(1, 2, 3)
8. book(1, 1, 3)
9. freeInRow(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 seatsPerRow 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 isFree, the seat is always inside the hall.
  • In freeInRow, 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 isFree or freeInRow should take O(1). Looking through every booking on each call would take O(n) time, for n bookings.

STEP 1 OF 35
    Ctrl + Enter to check

    Movie Booking in Go

    Keep going with Go1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free