#State#Components

Seat Booking Grid

A cinema seat picker: rows at different prices, sold seats, a selection limit and a running total.

By Pratik RaiMedium

Build the seat picker from a cinema booking page: seats laid out in rows at different prices, some already sold, a limit of four selections, and a running total.

This is one of the few machine-coding problems where layout and logic carry equal weight, which is why it gets used — a candidate who can only do one shows it within minutes.

On state, a Set of seat ids beats an array of seat objects. Every question the UI asks is membership, ids like B3 are derivable from the row and index without storing anything, and the summary needs nothing an id cannot look up. Copying the Set before mutating is the usual React rule: add returns the same object, so returning it re-renders nothing.

The selection limit has an asymmetry worth catching. Guarding the whole toggle means someone who has already picked four seats cannot deselect any of them — a genuinely broken UI. Only the add path is capped.

Prices living on the row rather than the seat means the total cannot be a count multiplied by one number. Flattening to a seat-id-to-price lookup once keeps the summary a reduce instead of a nested search back through the layout.

One small thing that reads as a bug if you skip it: list the selected seats in seat order, not click order.

Note that the source post describes the round only as "similar to the Book My Show ticket booking page" — the detailed requirements here are ours.

Goal: Select up to four available seats and total their prices correctly.

Source

Frequently asked questions

What is the best state shape for selected seats?
A `Set` of seat ids. Every question the UI asks is membership, and ids like `B3` are derivable from the row and index, so nothing extra needs storing. Copy the Set before mutating or React will not re-render.
Why should the selection limit only apply to adding?
Because guarding the whole toggle means someone who has picked the maximum cannot deselect anything and is stuck. Check the size only on the branch that adds a seat.
How do you total prices when rows cost different amounts?
Build a lookup from seat id to price once, then `reduce` over the selected ids. Searching back through the row layout for every selected seat works but does the same work repeatedly.
Why sort the list of selected seats?
Because seats listed in click order look like a bug when someone reads their own booking back. Sorting by seat id presents them the way the customer thinks about them.

Related Challenges

Continue learning with these related challenges

View All
React

Sequential Progress Bars

Clicking Add appends a progress bar, and the bars fill strictly one at a time. A queueing problem disguised as an animation.

React · JavaScriptPratik Rai ·

React

File Explorer

Build a nested folder tree from nothing — expand, collapse, and add files and folders at any depth.

React · JavaScriptPratik Rai ·

React

Pagination With Ellipsis

A pagination control that always shows the first, last and current pages, collapsing every gap into a single ellipsis.

React · JavaScriptPratik Rai ·