medium

Insert Interval

Insert one closed interval into a sorted list of disjoint closed intervals. Return a sorted disjoint list, merging overlaps including touching endpoints. Do not mutate the inputs.

Constraints
  • 0 ≤ n ≤ 10000
  • Each interval has start ≤ end
  • Endpoints are integers between 0 and 100000
Examples
in: [[[1,3],[6,8]],[2,7]]
out: [[1,8]]
Bridge two intervals
in: [[],[2,4]]
out: [[2,4]]
Empty list

Code it yourself

Solve in
Practice journal →Draft saved in this browser.
Public test cases · contract v1

Arguments are passed to your function. Tests are public practice checks, not hidden interview grading. Passing does not prove every possible input.

  • Bridge two intervals
    [[[1,3],[6,8]],[2,7]] → [[1,8]]
  • Empty list
    [[],[2,4]] → [[2,4]]
  • Insert before
    [[[5,8]],[1,2]] → [[1,2],[5,8]]
  • Contained
    [[[1,10]],[3,4]] → [[1,10]]
  • Touching endpoints merge
    [[[1,2],[5,6]],[2,5]] → [[1,6]]
Hints:
Which approach applies?

Choose an approach to check your pattern recognition, or reveal the discussion when you need help.