My first instinct was to just loop through every room and every interval inside it, which works but is obviously brute force.
For each room, use binary search to find the insertion point of the new meeting's start time, then check the previous and next intervals for overlap. Return the first room where no conflict exists, or -1 if none. This yields O(m log n) time where m is the number of rooms and n is the average number of intervals per room.
Pro tip: Mention that if the number of rooms is large and queries are frequent, you could preprocess each room's intervals into a balanced BST or use a segment tree to answer queries in O(log n) per room, but binary search is optimal for a single query. Also, clarify edge cases like empty rooms or intervals at boundaries.
Confirm that intervals are sorted and non-overlapping within each room, and that the new meeting must fit entirely without overlapping any existing booking. Ask about the expected number of rooms and intervals to choose the right algorithm.
For a given room, use binary search to find the index where the new meeting's start time would be inserted. Then check if the previous interval's end time is less than or equal to the new start, and the next interval's start time is greater than or equal to the new end.
Loop through each room, perform the check, and return the room index as soon as a valid insertion point is found. If no room works, return -1.
State that the time complexity is O(m log n) where m is the number of rooms and n is the average number of intervals per room, and space complexity is O(1) extra. Discuss potential optimizations if needed.
Consider cases like empty rooms, meetings that fit at the beginning or end of a room's schedule, and intervals that exactly touch (e.g., new end equals existing start). Walk through a small example to verify correctness.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.