CSE Roadmap

Core tier · about 2 hours · 7 parts

Part 5 of 7 · 1 min read

A small example

A meeting room booking prompt.

This level uses one prompt: "Design a system to book meeting rooms." Taking the first minute as an interviewer would:

  • Is a room booked by time only, or also by capacity and equipment?
  • Can bookings be cancelled? Can they repeat weekly?
  • Can two people book the same room at the same time? (The answer is almost always no.)
  • Are there many buildings, or one?

The central rule is that two bookings of the same room must not overlap. Times can be minutes from midnight, so 10:00 is 600 and 11:00 is 660. Two slots overlap when each starts before the other ends. The ends are exclusive, so a booking that ends at 11:00 and one that starts at 11:00 do not overlap.

record TimeSlot(int startMinute, int endMinute) {
    TimeSlot {
        if (startMinute >= endMinute) {
            throw new IllegalArgumentException("start must be before end");
        }
    }

    boolean overlaps(TimeSlot other) {
        return startMinute < other.endMinute && other.startMinute < endMinute;
    }
}

Checked with made-up values: 10:00 to 11:00 (600 to 660) and 10:30 to 11:30 (630 to 690) overlap, since 600 is below 690 and 630 is below 660. The slot 11:00 to 12:00 (660 to 720) does not overlap the first, since 660 is not below 660.

A record is a short Java form for a class that only holds values. It gives a constructor, accessors and equality for free. The block inside it, with no name, is a compact constructor that validates input.

Check yourself: 1. Why are the end times exclusive? 2. Do the slots 9:00 to 10:00 and 10:00 to 11:00 overlap? 3. What does the compact constructor stop?