Coding Pattern:

Intervals

This pattern has a cheat sheet you can unlock.

Pattern concept

Intervals Slide 1: An interval represents a range of values between two points on a number line. Here are three separate intervals: [0, 3], [1, 4], and [5, 7].
Intervals Slide 2: Intervals can overlap each other, like [0, 3] and [1, 4] do. How do you tell if two intervals overlap?
Intervals Slide 3: The easiest way to tell if two intervals overlap is to check that they are NOT non-overlapping.
Intervals Slide 4: Case 1: interval a ([0, 3]) starts and ends before interval b ([5, 7]), so they don't overlap.
Intervals Slide 5: Case 2: interval b ([0, 2]) starts and ends before interval a ([3, 6]), so they don't overlap.
Intervals Slide 6: Case 3: neither interval is entirely in front of the other, so [1, 5] and [3, 7] must overlap.
Intervals Slide 7: Sorting the list of intervals first usually makes it easier to step through. In Python that takes one line: intervals.sort(key=lambda x: x[0]). Sorting takes O(n log n) time.
Intervals Slide 8: The key is a function that takes an interval x and returns x[0], its start value. Sorting [[4, 6], [3, 5], [0, 2]] by start value gives [[0, 2], [3, 5], [4, 6]].
Intervals Slide 9: How do you find the intersection of two intervals?
Intervals Slide 10: The intersection of two overlapping intervals is [the larger start value, the smaller end value]. For [1, 5] and [3, 7], that's [3, 5].
Intervals Slide 11: How do you merge two intervals together?
Intervals Slide 12: The merged interval is [the smaller start value, the larger end value]. Merging [1, 5] and [3, 7] gives [1, 7].
1 / 12

Use ← → arrow keys

Code snippets

# Returns whether two intervals overlap.
def is_overlapping(interval_a: List[int], 
                   interval_b: List[int]) -> bool:
  a_start, a_end = interval_a[0], interval_a[1]
  b_start, b_end = interval_b[0], interval_b[1]

  if a_start < b_start and a_end < b_start:
    # Interval a starts and ends in front of interval b.
    return False
  elif  b_start < a_start and b_end < a_start:
    # Interval b starts and ends in front of interval a.
    return False
  else:
    # Interval a and b overlap in some way.
    return True
# Example of how to sort intervals.
def sort_intervals():
intervals = [[1, 3], [4, 5], [0, 7]]
intervals.sort(key=lambda x: x[0])
print(intervals)  # Returns [[0, 7], [1, 3], [4, 5]]
intervals.sort(key=lambda x: x[1])
print(intervals)  # Returns [[1, 3], [4, 5], [0, 7]]
# Get the intersection of two intervals.
def get_intersection(interval_a: List[int], 
                     interval_b: List[int]) -> List[int]:
  a_start, a_end = interval_a[0], interval_a[1]
  b_start, b_end = interval_b[0], interval_b[1]

  return [max(a_start, b_start), min(a_end, b_end)]
# Merge two intervals together.
def get_merged(interval_a: List[int], 
               interval_b: List[int]) -> List[int]:
  a_start, a_end = interval_a[0], interval_a[1]
  b_start, b_end = interval_b[0], interval_b[1]

  return [min(a_start, b_start), max(a_end, b_end)]

How to identify

Does the problem involve any of these?

Note

Most interval problems take one pass through the list, which is O(n) time. If you need to sort the intervals first, the total becomes O(n log n).