Exercise: The Stadium Seat Map
Take a ticket from Harbor Stadium's kiosk team: the festival's 200,000-place seat map has to fit in 10 MB on the gate tablets. Change the working seat map so seats of the same kind share their details, with the tests green throughout.
Freelancer · C# · Java · Python · JavaScript · TypeScript · Updated
Harbor Stadium's kiosk team has measured the problem for you: the seat map is too big for the tablets at the gates, and next summer's festival is four times the size of a game day. The code that's there works; your job is to make the map light enough for the festival, without breaking anything a fan sees today.
Before you start
You need one language set up, as in Setup: VS Code and Your Language, and to know how to run the tests, as in Running the Exercise Tests. The exercise is written in all five of the course's languages; pick the one you use.
Get the exercise code
Open this lesson's GitHub repository, github.com/ask-codey/exercise-dp-seat-map, also linked from the Exercise box beside the lesson. Clone it into your course folder. In a terminal, in that folder:
git clone https://github.com/ask-codey/exercise-dp-seat-map.git
Git makes a folder, exercise-dp-seat-map, with the exercise code inside.
The repository has a folder for each language: csharp, java, python, javascript and typescript. Each folder holds:
- the seat map's code, as it is today: a seat, its details, the ticket database's answers and the map itself, with the kiosk team's memory estimate (the repository's
README.mdnames the files for each language); - the tests, which you don't need to change;
- a demo program that builds today's 50,000-seat map and the festival's 200,000-place map.
In VS Code, choose File > Open Folder and open your language's folder, not the whole repository.
The ticket
From: the kiosk team, Harbor Stadium
- The festival map has to fit on the kiosk tablets. Next summer's festival has 200,000 places (800 rows of 250), and the tablets have room for about 10 MB of seat map. Our memory estimate,
estimatedBytes(), puts today's code at about 40 MB for the festival. It has to come in under 10 MB.- We think we know why. We count every
SeatDetailsthe map makes, and the festival map makes 200,000 of them, though there are only three kinds of seat. It should make at most three.- Seats of the same kind should share one details object.
Everything the map does today has to keep working: every seat's kind, price, color and perks, the counts of each kind, and each seat's row and number.
The memory estimate and the counter are already there; the kiosk team added them to measure the problem, and you don't need to change them.
What to do
- Run the tests first. Four pass: they're what the map does today. Three fail: they're the ticket.
- Run the demo, and read the code. Which of a seat's data is the same for thousands of seats? Which is truly its own? Where does each
SeatDetailsget made? - Change the code in small steps, and run the tests after every step. The four that pass must stay green the whole time.
- Stop when all seven are green and the demo prints what When you're done shows.
The README's Stuck? section names the pattern, if you want it. Try a few minutes without it first.
Check your work
Each language has its own command, run in the terminal in your language's folder (Terminal > New Terminal in VS Code). The repository's README.md has the full list, including the one-time setup for Python (a virtual environment and pytest) and TypeScript (npm install).
| Language | Run the tests | Run the demo |
|---|---|---|
| C# | dotnet test |
dotnet run --project StadiumSeats |
| Java | ./mvnw test (.\mvnw test in PowerShell) |
java src/main/java/Demo.java |
| Python | python -m pytest |
python demo.py |
| JavaScript | npm test |
node demo.js |
| TypeScript | npm test |
node demo.ts |
A failing test's message starts with Not yet: for the ticket's work, or Keep this working: if a change broke something that worked, and says what it expected, for example Not yet: two premium seats should share one SeatDetails object.
Hints
- Start with the counter. Every seat calls the ticket database for its details. What if something asked the database only once for each kind, and kept the answer?
- Then check what's shared. Once two premium seats point to the same details object, a change to one is a change to all 5,000. Should anything be able to change them?
- Leave each seat its row and number. Those really are its own.
When you're done
All 7 tests are green, and the demo prints the same four lines in every language:
Seats: 50000 (standard 44640, premium 5000, accessible 360)
Row 12, seat 7: premium, $90, gold
Festival map: 200000 places, about 8.0 MB
Seat details made: 3
The festival map went from about 40 MB to about 8 MB, and the 50,000-seat map and the festival map together made three seat details instead of 250,000. Every fan still sees the same prices, colors and perks.
Submit your exercise
The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here.
On GitHub, click +, then New repository. Name it (for example
exercise-dp-seat-map), choose Public, and click Create repository. Leave out the README: your folder already has one.In a terminal in your exercise folder (Git Bash on Windows), send your work to your new repo. Point your copy at your own repo, then commit and push:
git remote set-url origin https://github.com/your-name/exercise-dp-seat-map.git git add . git commit -m "My solution" git push -u origin mainPaste your repo's address, such as
https://github.com/your-name/exercise-dp-seat-map, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.
Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.
So what
The map's size now grows with its seats' rows and numbers, not with copies of the same three details, so the festival fits on a tablet. Module 7 is the course's last pass: an old gym's class-booking system that the whole business depends on. You'll read its code smells as symptom, cause and cure, change it safely under tests, replace it piece by piece, and finish by finding and naming patterns of your own.