Skip to main content

50. Merge Intervals

mediumAsked at Workday

Given a collection of intervals, merge all overlapping intervals. Workday uses this canonically for time-and-attendance — same shape as merging overlapping clock-in/out windows to compute total worked hours.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Workday loops.

  • Glassdoor (2026-Q1)Workday time-and-attendance team — direct domain question.
  • Blind (2025)Workday onsite phone for HR-team SDE2.

Problem

Given an array of intervals where intervals[i] = [start_i, end_i], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.

Constraints

  • 1 <= intervals.length <= 10^4
  • intervals[i].length == 2
  • 0 <= start_i <= end_i <= 10^4

Examples

Example 1

Input
intervals = [[1,3],[2,6],[8,10],[15,18]]
Output
[[1,6],[8,10],[15,18]]

Example 2

Input
intervals = [[1,4],[4,5]]
Output
[[1,5]]

Explanation: Intervals [1,4] and [4,5] are considered overlapping.

Approaches

1. Pairwise check unsorted

For every pair, check overlap; union-find merge.

Time
O(n^2)
Space
O(n)
// quadratic — works but wasteful

Tradeoff: Quadratic. Sort enables linear scan.

2. Sort by start, merge in one pass

Sort intervals by start. Walk; if current overlaps last in output, extend end. Else push.

Time
O(n log n)
Space
O(1) extra)
function merge(intervals) {
  intervals.sort((a, b) => a[0] - b[0]);
  const result = [];
  for (const [s, e] of intervals) {
    if (result.length === 0 || result[result.length - 1][1] < s) {
      result.push([s, e]);
    } else {
      result[result.length - 1][1] = Math.max(result[result.length - 1][1], e);
    }
  }
  return result;
}

Tradeoff: Sort + linear pass. The key: once sorted by start, you only need to compare current to the most recent merged interval.

Workday-specific tips

Workday LOVES this question because of the direct time-and-attendance analogy. Mention it. Edge case to call out: '[1,4] and [4,5] overlap' — the boundary tie is inclusive. Get this confirmed with the interviewer before coding.

Common mistakes

  • Strict inequality (result.end < s) vs result.end <= s — depends on whether touching intervals merge.
  • Forgetting to sort first.
  • Pushing every interval without checking — produces unmerged output.

Follow-up questions

An interviewer at Workday may pivot to one of these next:

  • Insert Interval (LC 57).
  • Meeting Rooms II (LC 253) — minimum rooms required.
  • Non-overlapping Intervals (LC 435).

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Touching intervals — overlap?

Depends on the problem. LC 56 considers [1,4] and [4,5] as overlapping. Always confirm with the interviewer.

Why sort by start, not end?

Sorted by start makes overlap detection a simple last-end vs current-start comparison. Sorted by end works for different problems (interval scheduling).

Practice these live with InterviewChamp.AI

Drill Merge Intervals and other Workday interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →