Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close

    Coding problem

    Cancel the fewest bookings

    MediumAbout 25 minFull AccessLearn it: Interval Problems in C#: Merge, Insert, Greedy Scheduling, Meeting Rooms and Sweep Lines

    The task

    A meeting room was double-booked by mistake, and the office wants to cancel as few bookings as possible so that the rest never overlap.

    Complete FewestRemovals(int[][] intervals). Each booking is [start, end] with start < end, and it holds the room from start up to, but not including, end: a booking that ends at 5 and one that starts at 5 do not overlap. Return the fewest bookings to cancel so that no two of the remaining bookings overlap. The bookings come in any order, and the array is never null.

    Signature

    public static int FewestRemovals(int[][] intervals)

    Examples

    • [[1, 100], [2, 3], [3, 4], [4, 5]] → 1. Cancelling the long booking lets the three short ones stay.
    • [[4, 6], [4, 6], [4, 6]] → 2. Only one of three equal bookings can stay.

    Limits: Up to 300,000 bookings; times can be any int.

    The tests check: no bookings, one booking, touching bookings, bookings in any order, negative times, int.MinValue and int.MaxValue as times, ends far apart, and 300,000 bookings in time.

    Hints

      Your code

      C#Solution.csLoading the editor…

      Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;

      public static class Solution
      {
          // The fewest bookings [start, end) to cancel so that the rest never overlap.
          public static int FewestRemovals(int[][] intervals)
          {
              throw new NotImplementedException();
          }
      }

      This problem comes with TechInterviewNotes Full Access

      Unlock all paid content on TechInterviewNotes.