Design a parking lot
medium 90 minModel a car park with several levels as classes: vehicles, spaces of different sizes, tickets and a replaceable price rule.
Watch lesson4:48
How to approach it
What this question tests, where it gets hard, and how to spend the time.
Read the question
What this problem tests
Can you cut?
Payment, booking, electric, valet: the prompt invites everything. Choose two verbs and do them well.
Can you find the owner?
Who may hand out a space? One answer makes the rest easy.
Can you leave classes out?
Say which nouns you reject. It sounds like experience because it is.
Can you see the gap?
Between looking and taking, and between charging and releasing.
Think
Five questions before you draw
| Ask yourself | Here |
|---|---|
| Who does what? | A driver enters and leaves. A sign reads counts |
| What must never go wrong? | One space for two cars, and a car that cannot leave |
| What is the hardest thing? | Claiming, when several gates want the last space |
| What changes most? | Prices |
| What am I giving up? | An exact total across levels, if I split the lock |
Time
Spend the 45 minutes
| Minutes | Do | Leave with | Skip |
|---|---|---|---|
| 0 to 5 | Ask, write down the answers | Two verbs, three sizes, several gates | Booking, passes |
| 5 to 10 | Nouns: keep and reject | Seven boxes and the reasons | Getters |
| 10 to 18 | Class diagram | State and public methods | Both directions of each arrow |
| 18 to 33 | Code: pool, enter, leave | Runs in your head | Printing, parsing |
| 33 to 38 | Trace: full lot, declined card | One bug found by you | "It should work" |
| 38 to 45 | Races, then one extension | Which class changes | A second full design |
If you are stuck, go round this loop
Memory
PARK
Participants: vehicle, space, lot, ticket. Actions: enter, leave, claim, release. Relationships: the lot owns the pool, the ticket links vehicle and space. Key risks: the race, the price, the payment. Say them in this order and you have an outline.
Lines that help
- "A level is a number until it owns something."
- "Full is a result, not an exception."
- "I will take the ticket out before I charge."
- "Here is the sum that says one lock is enough."
Lines that hurt
- "I will make it a Singleton, because there is one lot."
- "I will add a factory, so it is extensible."
- "We can make all methods synchronized." And the card?
- "Then we put it in a database." That was not the question