Meeting Scheduler

Asked atMeta
1Give yourself 5 minutes
2Answer out loud, not in your head
3Then compare with the answer below

The problem

You are given two lists of availability slots for two people. Each slot is a list of two integers [start, end], representing the inclusive start time and the exclusive end time of that person's available time.

Your task is to find the earliest time slot that is at least **duration **minutes long and is common between both people's availability. If there is no such slot, return an empty list.

Input: slots1 = [[10, 50], [60, 120], [140, 210]], slots2 = [[0, 15], [60, 70]], duration = 8 **Output: **[60, 68] **Explanation: ** The only overlapping slot that is at least 8 minutes long is [60, 68].

Input: slots1 = [[10, 50], [60, 120], [140, 210]], slots2 = [[0, 15], [60, 70]], duration = 12 Output: [] Explanation: Although [60, 70] overlaps, it is only 10 minutes long, which is less than the required 12 minutes.

Input: slots1 = [[10, 20], [30, 40]], slots2 = [[15, 25], [35, 50]], duration = 5

  • 1 <= slots1.length, slots2.length <= 104
  • slots1[i].length, slots2[i].length == 2
  • slots1[i][0] < slots1[i][1]
  • slots2[i][0] < slots2[i][1]
  • 0 <= slots1[i][j], slots2[i][j] <= 109
  • 1 <= duration <= 106

cpp

class Solution {
public:
    vector<int> minAvailableDuration(vector<vector<int>>& slots1, vector<vector<int>>& slots2, int duration) {
        //Your Code Goes Here
    }
};

java

class Solution {
    public List<Integer> minAvailableDuration(int[][] slots1, int[][] slots2, int duration) {
        //Your Code Goes Here
    }
}

python

class Solution(object):
    def minAvailableDuration(self, slots1, slots2, duration):
        """
        :type slots1: List[List[int]]
        :type slots2: List[List[int]]
        :type duration: int
        :rtype: List[int]
        """
        //Your Code Goes Here

javascript

/**
 * @param {number[][]} slots1
 * @param {number[][]} slots2
 * @param {number} duration
 * @return {number[]}
 */

class Solution{
    minAvailableDuration(slots1, slots2, duration){
        //your code goes here
    }
}

csharp

public class Solution
{
    public IList<int> MinAvailableDuration(int[][] slots1, int[][] slots2, int duration)
    {
        //Your Code Goes Here
    }
}

go

func minAvailableDuration(slots1 [][]int, slots2 [][]int, duration int) []int {
    // Your code goes here
}
Stuck? Show a way to structure it+
  1. 01Point at the first slot in each list.
  2. 02Compute overlap start as the maximum start.
  3. 03Compute overlap end as the minimum end.
  4. 04Return when the overlap fits duration.
  5. 05Advance the interval that ends first.

Reference answer

Then expect these follow-ups

  • How would you support more than two calendars?

    Tests: heap merge

  • How would you find all feasible slots?

    Tests: iteration

Free to read · better with Enzo

Practice this out loud with Enzo

Enzo runs it as a mock interview, pushes back with follow-ups, and grades you on the rubric.

Next question