Ask before you draw
After this: Ask the questions that settle what the parking lot must do, and agree on a short paragraph of requirements.
On this page · 7 sections · 3 checks
An interviewer says four words: "Design a parking lot." Where do you start? Many people start drawing boxes at once. Ten minutes later they learn that the interviewer wanted reservations, or only cars, and the drawing is wrong. This tutorial takes one design from those four words to a program that runs, one careful step at a time.
The five steps
The same steps work for any small design task. Each lesson of this tutorial does one of them:
- Ask questions until you agree what the program must do (this lesson).
- Mark the nouns and the verbs in the agreed text: nouns become classes, verbs become methods (lesson 2).
- Draw the classes and the lines between them (lesson 3).
- Write the code from the drawing, and run it (lesson 4).
- Follow one action through the objects, one call at a time, to check that the design works (lesson 5).
What a requirement is
A is one sentence that says what the system must do. "When a vehicle enters, the lot gives a ticket" is a requirement.
Requirements come in two kinds. A says what the system does: it gives a ticket, it works out a fee. A says how well it does it: how fast, for how many cars at once, without ever giving one spot to two cars. This tutorial designs for the functional ones and names the others at the end of lesson 5.
The questions to ask
"Design a parking lot" leaves almost everything open. Ask about the things that would change your classes or your rules:
| Question | The answer we agreed |
|---|---|
| Which vehicles park here? | Bikes, cars and trucks. |
| What sizes of spot are there? | Small, medium and large. |
| Must each vehicle use its own size? | Its own size first; a bigger spot when none of its own size is free. |
| Is there more than one level? | Yes, several. |
| How is the fee worked out? | By the hour, at a rate for each kind of vehicle. |
| How do drivers pay? | Not part of this design. |
| Can a driver reserve a spot? | No. |
| What must the lot be able to say? | How many spots of each size are free. |
The agreed paragraph
After the questions, write the answers down as one short paragraph, and read it back to the interviewer. This is ours, and the next lessons build on its exact words:
A parking lot has several levels. Each level has spots of three sizes: small, medium and large. A bike needs a small spot, a car a medium spot and a truck a large spot; a vehicle may use a bigger spot when none of its own size is free. When a vehicle enters, the lot gives a ticket that records the vehicle, the spot and the entry time. When it leaves, the driver pays for every started hour: 10 rupees for a bike, 20 for a car and 50 for a truck. The lot can say how many spots of each size are free.
The paragraph still leaves a few things open. Code needs one answer to each, so we agreed four rules for the code too:
- Search the levels in order: level 1 first.
- On a level, take the smallest free spot the vehicle fits. A car takes a free medium spot before a large one, so the large spot stays free for a truck.
- Times are whole minutes since the lot opened. Minute 0 is opening time, minute 61 is one hour and one minute later.
- The fee is the rate times the number of started hours, and at least one hour.
Here is rule 4 on three small cases, for a car at 20 rupees an hour:
- It stays 60 minutes: one hour has started. It pays 1 × 20 = 20.
- It stays 61 minutes: a second hour has started. It pays 2 × 20 = 40.
- It stays 0 minutes: no hour has started, but the minimum is one hour. It pays 20.
What we leave out
The of a design is what it covers. Writing down what is out of scope is as useful as writing down what is in it. For this lot, these are out of scope: taking card or cash payments, reserving spots, sensors that see a car arrive, and display boards. Each one could be added later. The final challenge adds two new requirements and shows where they go.
Check yourself
An interviewer says: "Design a parking lot." Which of these is the most useful thing to say first?
Which of these is out of scope for the design in this tutorial?
A car arrives. On level 1, a medium spot and a large spot are both free. The paragraph does not say which one it gets. Which spot does it get under the rules we agreed for the code?
In an interview
- Start a design by asking questions whose answers change the classes or the rules: which vehicles, which spot sizes, how fees work.
- Write the answers as one short agreed paragraph of requirements, and read it back.
- Tell a functional requirement (what the system does) from a non-functional one (how well it does it).
- Add rules for the code where the paragraph leaves a choice open: levels in order, the smallest spot that fits, whole minutes, at least one started hour.
- Write down what is out of scope, such as card payments and reservations.