Part 6 of 7 · 2 min read
Step 5
Look for failures on purpose.
Step 5: edge cases and tests
Edge cases come from a short checklist (common practice), not from luck:
| Check | For the room system |
|---|---|
| Empty or null input | A null room or user |
| Boundaries | Slots that touch end to start |
| Duplicates | The same booking twice |
| State | Cancelling a cancelled booking; booking a cancelled slot |
| Order | Two bookings made at the same instant |
| Size | Thousands of bookings in a room |
Some are handled in the code above: touching slots do not overlap, a cancelled booking is skipped by the loop, and an unknown id throws an exception. Others are not. Two threads calling book at once could both pass the check. Saying "this needs a lock, and Level 5 shows how" is a good answer when time is short.
Then a short test list: book into an empty room works; booking an overlapping slot fails; touching slots both succeed; cancel then rebook works; cancelling an unknown id fails.
Change requests. The interviewer may now ask for something new, such as "show free rooms for a slot". Point to the class that changes: BookingService gets a method that loops over rooms and calls the same overlap check. TimeSlot, Room and Booking stay the same. A change that touches one class is a sign the design is working.
Common mistake
Treating edge cases as an afterthought. Name them in step 1 and mention them again at step 5.
Interview angle
Answer in your head first, then open it.
Walk me through your approach.
Requirements, entities, classes and links, main flow in code, then edge cases and tests.
What if two people book at the same moment?
The check and the add must happen as one step, with a lock or a thread-safe structure.
Show me how easy it is to add a feature.
Name the class that changes and say what stays the same.
Check yourself: 1. Which two edge cases does the code already handle? 2. Which edge case needs a lock? 3. How does a change touching one class reflect on the design?
Words you will meet
| Word | Meaning |
|---|---|
| Main flow | The most important path, coded first |
| Edge case | An unusual input or state that can break the code |
| Responsibility | The one job a class is meant to do |
| Change request | A new requirement added during the round |
| Exception | A Java signal that something went wrong |