Java traps · Chapter 5 of 16
A mutable key in a HashMap
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Everyday traps
- 3 tasks
- Java
- In the all labs plan
The first 2 chapters of Java traps are free. This one comes with the all labs plan.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Repro.java: an order lost when its coach is renumbered
Write reproduce() in Repro.java. Make a SeatBoard, place masala chai at coach B2, seat 34, renumber coach B2 to B3 with renumberCoach, and return what orderAt gives for coach B3, seat 34.
The check runs your reproduce() twice: beside the original SeatBoard.java it must return null (the order is on the board but cannot be found), and beside a fixed SeatBoard.java it must return "masala chai". So it must really place the order, renumber the coach and look it up. No real waiting and no reading input: reproduce() must finish within 3 seconds.
- Task 2
SeatBoard.java: orders follow a renumbered coach
Fix renumberCoach(from, to) in SeatBoard.java so that afterwards every order of the old coach is found by orderAt at the new coach and the same seat, orderAt at the old coach gives null, orders in other coaches are as they were, pending() lists the same number of orders as before (each once, under its new coach), and placing an order at a renumbered seat replaces the order there instead of adding a second one. Renumbering a coach with no orders changes nothing.
Keep place, orderAt and pending as they are, and the log lines: seat board: <coach>-<seat> <item>, and seat board: coach <from> is now <to>, <n> orders moved. The check uses its own coaches and seats, renumbers some twice, and counts the orders after renumbering.
- Task 3
SeatBoardTest.java: a test that would have caught it
Write a JUnit test class, SeatBoardTest.java, that would have caught this bug before it reached the stations. The check runs your file three times: beside the original SeatBoard.java, where at least one test must fail; beside the fixed SeatBoard.java, where every test must pass; and beside a wrong fix that renames the seat in place and puts the order in again, where at least one test must fail.
So test both sides after renumbering a coach: an order is found at its new coach (and not at the old one), and the board holds the same number of orders as before. No real waiting: each test must finish within 3 seconds.
What you will learn
- Repro.java: an order lost when its coach is renumbered
- SeatBoard.java: orders follow a renumbered coach
- SeatBoardTest.java: a test that would have caught it
More in Everyday traps
Build "A mutable key in a HashMap" yourself
Tapri Counter runs on Java, and Kabir, its senior developer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Java traps are free. This one comes with the all labs plan.