Merge Intervals Mock Interview

  1. Problem
  2. 2Clarifying Questions
  3. 3Constraints
  4. 4Brute Force
  5. 5Complexity Analysis
  6. 6Pattern Recognition
  7. 7Optimized Solution
  8. 8Implementation
  9. 9Testing
  10. 10Follow-Up
  11. 11Evaluation
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. Two intervals that merely touch (one ends where the other starts) are considered overlapping.

Constraints
  • 1 ≤ n ≤ 10^4
  • 0 ≤ start_i ≤ end_i ≤ 10^4
  • input is not necessarily sorted
Example
in: intervals = [[1,3],[2,6],[8,10],[15,18]]
out: [[1,6],[8,10],[15,18]]

Clarify

Before choosing anything: what would you ask the interviewer? What assumptions are you making? (Duplicates? Empty input? Value ranges? What to return when there is no answer?)