SDE Path

Movie Ticket Booking

Medium

Movie Ticket Booking

Book seats for a movie show: register shows with a seat grid, reserve individual seats (no double-booking), and report how many seats are left.

Requirements

  • addShow registers a show with a rows×cols grid of seats, all initially free. Show ids are distinct.
  • Seats are addressed as R<row>C<col>, 1-indexed (e.g. R2C3 is row 2, column 3). Parse the token exactly: an uppercase R, one or more digits, an uppercase C, one or more digits, and nothing else — anything that doesn't match this shape is invalid.
  • book returns true only when the show exists, the token is well-formed, the seat is in range, and it is currently free; it then marks that seat booked. Every other case (malformed token, out-of-range seat, already-booked seat, unknown show) returns false and changes nothing.
  • available returns the number of free seats and 0 for an unknown show.

API to implement

Implement the class MovieBooking. The I/O driver is already written in the starter code — it parses the command script below and calls your methods. You only implement the class.

| Command | Returns | Behavior | |---|---|---| | addShow <showId> <rows> <cols> | — | Register show showId with a rows×cols grid of seats, all initially free. Show ids are distinct. | | book <showId> <seat> | boolean | Reserve seat (like R2C3, 1-indexed) in show showId. Returns true only if the show exists, the token is well-formed, the seat is in range, and it is currently free — then marks it booked. Any other case (bad format, out of range, already booked, unknown show) returns false. | | available <showId> | int | Number of free seats in show showId. Returns 0 for an unknown show. |

Input format

The first line contains Q, the number of operations. Each of the next Q lines is one command as listed above. String arguments contain no spaces.

Output format

For each command with a non-void return, print its result on its own line, in order. The driver does this for you — your methods just return values.

This is a design problem: the hidden tests exercise edge cases and interaction sequences, not performance tricks. Model the state cleanly and the tests will pass.

Sample cases

Example 1
Input
7
addShow 1 2 3
available 1
book 1 R1C1
available 1
book 1 R1C1
book 1 R2C3
available 1
Expected output
6
true
5
false
true
4
Example 2
Input
7
addShow 7 2 2
book 7 R3C1
book 7 R1C3
book 7 R0C1
book 7 XYZ
book 7 R1C1
available 7
Expected output
false
false
false
false
true
3