Merge overlapping ranges and report how many remain.
Acceptance criteria
- 1.#out should show merged into 3.
DSA Tutorial · Intervals
These 10 tasks go with the Merging and Sorting Intervals chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “Meeting rooms, calendar bookings, memory ranges, video segments. They are all the same problem: pairs of numbers on a line that may or may not overlap.” .
1.Merge overlapsexercise · easy · 10 min
Merge overlapping ranges and report how many remain.
Passes when: #out should show merged into 3.
2.Test an overlapexercise · easy · 10 min · practice plan
Report whether two ranges overlap.
Passes when: #out should show overlapping.
3.Touching endpointsexercise · easy · 10 min · practice plan
Report whether ranges that meet at a point overlap.
Passes when: #out should show separate.
4.Sweep for concurrencyexercise · easy · 10 min · practice plan
Find the greatest number of ranges active at once.
Passes when: #out should show peak 2.
5.Widest merged rangeexercise · medium · 10 min · practice plan
Show the end of the first merged range.
Passes when: #out should show ends at 6.
6.Fix the merge without sortingbug fix · easy · 8 min
Unsorted ranges cannot be merged in a single pass.
Passes when: #out should show merged into 2.
7.Fix the merged end assigned directlybug fix · medium · 8 min · practice plan
A contained range must not shrink the one already merged.
Passes when: #out should show ends at 9.
8.Fix the overlap test using orbug fix · easy · 8 min · practice plan
Both conditions must hold for two ranges to overlap.
Passes when: #out should show separate.
9.Fix the sweep tie-breakbug fix · easy · 8 min · practice plan
A range ending at the same instant frees its slot before the next begins.
Passes when: #out should show peak 1.
10.Fix the merge mutating the inputbug fix · easy · 8 min · practice plan
Pushing the original array lets a later change alter the source.
Passes when: #out should show source intact.
Between them these tasks cover Sorting by start, Overlap tests and Sweep line.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.