Interval, scheduling and calendar coding interview questions
Eight interval and scheduling coding questions in Java and Python, from merging and arrows to calendars, rooms and time-window counts.
Merging and insertion
Sort by start and merge overlaps, or insert one interval into an already-sorted list.
1.1 Merge Intervals Medium 3 min
I sort the intervals by start time, then I walk them once and either extend the current interval when the next one overlaps it, or I close the current interval and start a new one. Two intervals overlap when the next start is at most the current end.
Read the full answer1.2 Insert Interval Medium 4 min
I walk the already-sorted list once. I copy every interval that ends before the new one starts, then I merge the new interval with every interval that overlaps it, and I append the rest. No extra sort is needed because the input is already ordered.
Read the full answer1.3 Non-overlapping Intervals Medium 3 min
I sort the intervals by end time and then I keep a interval whenever it starts at or after the last kept end. Every interval I skip is one I have to remove. This is the classic greedy choice for the maximum number of non-overlapping intervals, so the removals are n minus that count.
Read the full answer1.4 Minimum Number of Arrows to Burst Balloons Medium 4 min
I sort the balloons by end position and I shoot an arrow at the end of the first balloon. Every later balloon that starts at or before that arrow is burst by the same shot. When a balloon starts after the arrow, I need a new arrow at that balloon's end. The number of arrows is the number of groups I form this way.
Read the full answer1.5 Meeting Rooms II Medium 3 min
I sort the start times and the end times separately, then I walk both lists. When the next meeting starts before the earliest current end, I need a new room. When a start is at or after an end, a room has freed and I advance the end pointer. The maximum number of rooms I have opened is the answer.
Read the full answer1.6 My Calendar I Medium 3 min
I keep the booked intervals in a sorted map from start to end. To book [start, end) I look at the neighbouring interval on the left and on the right. The new booking is legal if the previous end is at most start and the next start is at least end, because the intervals are half-open.
Read the full answer1.7 My Calendar II Medium 4 min
I keep every accepted booking, and a second list of the ranges that are already double-booked. A new interval is rejected only if it overlaps a double-booked range, because that would be a triple booking. Otherwise I add every overlap with an existing booking to the double-booked list, then I store the new booking.
Read the full answer1.8 Tweet Counts Per Frequency Medium 5 min
I store the timestamps of each tweet name in a sorted list. For a query I walk the closed range [startTime, endTime] in chunks of 60, 3600 or 86400 seconds, and I count how many stored times fall into each chunk, including empty chunks at the end.
Read the full answer