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
Explain your choices
Answer first. Then compare your reasoning.
15 questions 0 got it 0 to review15 not marked
Defend
Choice, reason, cost, trigger
| Choice | Reason | Cost | I would change it when |
|---|---|---|---|
| Enum for vehicles | They differ by one value | No behaviour per type | Types act differently |
No isFree on a space | One fact, one home | Asking one space needs a lookup | Sensors report per space |
| One lock | 0.00085% busy | All gates share it | A profile says so |
| Pop, charge, put back | No lock during a card | A ticket is nowhere for seconds | Status in a database |
| Price by space | The sign shows prices for sizes | A motorcycle moved up pays more | Customers complain: one word |
| Counter for ids | Short, sorted | Guessable | The paper opens a barrier |
| Size before distance | Buses find room | Longer walks for cars | No buses come |
The interviewer says: make it a Singleton.
Breaks"Yes, with getInstance"
- How
- Global access, private constructor.
- Limit
- Every class can reach it, tests share it, and it needs a thread-safe start.
- Choose when
- Only if they insist. Then write the holder version.
Works"No, Singletons are bad"
- How
- Refuse by rule.
- Limit
- A rule is not a reason. They asked for one.
- Choose when
- Not enough alone.
Strong"One instance yes, one global no"
- How
- Create it in
main, pass it to the gates. Offer the safe Singleton if they still want it. - Limit
- A longer constructor call.
- Choose when
- Say what breaks: the second car park, and the second test.
Scan, queue, heap, or database?
BreaksScan for every arrival
- How
- Walk over all spaces.
- Limit
- Slowest when full, and it needs a lock the whole time.
- Choose when
- A toy.
WorksQueues by size
- How
- O(1), no distance.
- Limit
- Any space, not the nearest.
- Choose when
- First answer.
StrongThe structure for the question they asked
- How
- Queue for any, heap for nearest, bits for runs, conditional update for many machines.
- Limit
- More than one structure must be kept equal.
- Choose when
- After they say which one matters.