Java · Coding problem

Book a Meeting Room

A free coding problem from the Java: Collections and Recursion skill. Read it here, then solve it in the app in Java.

  • Easy
  • Coding problem
  • Free
  • Java

The problem

A coworking space in Dublin lets members book its one meeting room. Each request asks for the time range [start, end): it includes start and stops just before end, so a meeting from 10 to 20 and another from 20 to 30 don't clash.

Implement book(start, end). If the range overlaps no booking you've already accepted, accept it and return true. Otherwise reject it and return false (a rejected request isn't stored).

Example

book(10, 20) -> true
book(15, 25) -> false   (overlaps 10-20)
book(20, 30) -> true    (starts exactly when 10-20 ends)

Input: the number of requests, then one start end pair per line. Output: true or false for each request, one per line.

Checking every accepted booking works, but it's O(n) per request. Keep the bookings sorted by start time instead, so you only need to look at the booking just before and the booking just after: O(log n).

Constraints

  • 1 <= number of requests <= 10^4
  • 0 <= start < end <= 10^9

How it's graded

  • Run checks your code against the examples. Submit also runs hidden tests, and you see pass or fail for them, never their inputs.
  • Stuck? Hints come in four steps, from a nudge to a code skeleton, each costing part of the points.
  • Once you pass, you see the approaches from brute force to optimal, and where your solution lands among them.

More problems in Java: Collections and Recursion

Solve it Book a Meeting Room free

No card needed. Lessons, practice questions and easy problems are free forever.

Free plan, no card · 7-day Pro trial · Cancel anytime