Practice

Problem 7 of 12

Problem · Medium · 25 min

Merge the time slots

  • Medium
  • Arrays
  • Sorting

A calendar holds time slots. Each slot is an array [start, end] of two whole numbers with start <= end. Slots can overlap.

Write mergeRanges(ranges). It returns a new array of slots, where every group of overlapping slots became one slot.

  • Two slots overlap when they share at least one point. [1, 3] and [3, 5] share the point 3, so they become [1, 5].
  • A slot inside another slot disappears into it.
  • Sort the result by start. No two slots in the result overlap.
  • The input can be in any order.

Examples

Call Result
mergeRanges([[1, 3], [2, 6], [8, 10], [15, 18]]) [[1, 6], [8, 10], [15, 18]]
mergeRanges([[1, 3], [3, 5]]) [[1, 5]]
mergeRanges([]) []

Tasks

2 to do

  1. Task 1, not checked yet. Make mergeRanges pass the example tests.

  2. Task 2, not checked yet. Make mergeRanges pass the hidden tests.

Task 1 of 2

Make mergeRanges pass the example tests.

All tasks

main.js

Ctrl+Enter runs your code. Ctrl+Shift+Enter runs all the tests. Press Esc, then Tab, to leave the editor.

Output

Press Run to see your result

Your output shows here. Press Run to try your code.