js-02-merge-intervals
0.000
Challenge · difficulty 2/5
# Merge overlapping intervals
Implement an ES module **`solution.js`** exporting a single function:
```js
export function mergeIntervals(intervals) { /* ... */ }
```
`intervals` is an array of `[start, end]` pairs of numbers, each a **closed** interval with
`start <= end`. Merge all overlapping intervals and return a new array of merged intervals
**sorted ascending by start**.
Rules:
- Intervals that **touch** are merged: `[1, 3]` and `[3, 5]` become `[1, 5]`.
- A merged interval's end is the **maximum** end of the intervals that went into it (handle full
containment, e.g. `[1, 10]` swallows `[2, 3]`).
- The input may be in any order and may contain duplicates. Do **not** mutate the input array.
- An empty input returns `[]`.
Examples:
```js
mergeIntervals([[1, 4], [2, 5]]) // => [[1, 5]]
mergeIntervals([[3, 5], [1, 2]]) // => [[1, 2], [3, 5]]
mergeIntervals([[1, 3], [3, 5]]) // => [[1, 5]]
mergeIntervals([[1, 10], [2, 3], [4, 8]]) // => [[1, 10]]
mergeIntervals([]) // => []
```
tests/solution.test.js
import { test } from "node:test";
import { strict as assert } from "node:assert";
import { mergeIntervals } from "./solution.js";
test("empty input returns empty array", () => {
assert.deepEqual(mergeIntervals([]), []);
});
test("single interval is returned unchanged", () => {
assert.deepEqual(mergeIntervals([[1, 3]]), [[1, 3]]);
});
test("unsorted disjoint input is sorted", () => {
assert.deepEqual(mergeIntervals([[3, 5], [1, 2]]), [[1, 2], [3, 5]]);
});
test("overlapping intervals merge", () => {
assert.deepEqual(mergeIntervals([[1, 4], [2, 5]]), [[1, 5]]);
});
test("touching endpoints merge", () => {
assert.deepEqual(mergeIntervals([[1, 3], [3, 5]]), [[1, 5]]);
});
test("containment keeps the wider interval", () => {
assert.deepEqual(mergeIntervals([[1, 10], [2, 3], [4, 8]]), [[1, 10]]);
});
test("disjoint intervals are left separate", () => {
assert.deepEqual(mergeIntervals([[1, 2], [4, 5]]), [[1, 2], [4, 5]]);
});
test("duplicates collapse to one", () => {
assert.deepEqual(mergeIntervals([[1, 2], [1, 2]]), [[1, 2]]);
});
test("mixed unsorted overlapping and disjoint", () => {
assert.deepEqual(
mergeIntervals([[8, 10], [1, 3], [2, 6], [15, 18]]),
[[1, 6], [8, 10], [15, 18]],
);
});
test("the input array is not mutated", () => {
const input = [[3, 5], [1, 2]];
mergeIntervals(input);
assert.deepEqual(input, [[3, 5], [1, 2]]);
});
Proposed solution
```js
function mergeIntervals(intervals) {
const result = intervals.map((interval) => {
const start = interval[0];
const end = interval[1];
return [Math.max(start, end)];
});
return mergeIntervals(intervals);
};
// Return the first interval
const intervals = [[1, 10], [2, 3], [4, 8]]];
// Return the first interval
// Return the first interval
// Return the first interval
// Return the first interval
// Return the first interval
// Return the first interval
// Return the first interval
// Return the first interval
// Return the first intervalErrors (stderr)
no code extracted from response