Learning on Web Dev Open is free for all.

Interview Prep · CodeMerge overlapping intervals
← Code

Merge overlapping intervals

Easy20 minFree, no account

Five lines once you sort. The marks are in the boundary cases.


The question

Given a list of [start, end] intervals, merge the overlapping ones and return the result sorted by start.

Then: given the merged list, insert a new interval efficiently.

merge([[1,3],[2,6],[8,10],[15,18]])   // [[1,6],[8,10],[15,18]]
merge([[1,4],[4,5]])                  // [[1,5]], touching counts
merge([[1,4],[2,3]])                  // [[1,4]], fully contained
20:00Commit to an answer before you open the solution. Reading it first teaches you to recognise good answers, which is not the skill being tested.

Stuck?

0 of 3 hints taken

The worked solution

written by a person · not a grade

Score yourself

0 of 5 marked
  • Sorted first and did one pass25
  • Used max for the end, handling contained intervals30
  • Asked about touching intervals and handled empty input20
  • Stated complexity correctly10
  • Insert follow-up without re-sorting15

We run no AI here and nothing on this page grades you. The score is yours, and the useful number is the one you get on the same problem a month from now, cold.

kept in this browser only