Bird
Raised Fist0

Consider the following Python code that merges intervals in-place after sorting. Given the input intervals = [[1,4],[2,5],[7,9]], what is the returned list after the function completes?

easy🧾 Code Trace Q12 of Q15
Intervals - Merge Intervals
Consider the following Python code that merges intervals in-place after sorting. Given the input intervals = [[1,4],[2,5],[7,9]], what is the returned list after the function completes?
A[[1,5],[7,9]]
B[[1,4],[2,5],[7,9]]
C[[1,5],[2,5],[7,9]]
D[[1,4],[7,9]]
Step-by-Step Solution
  1. Step 1: Sort intervals by start time.

    Input is already sorted: [[1,4],[2,5],[7,9]].
  2. Step 2: Merge intervals in one pass.

    Compare [2,5] with [1,4]: overlap since 2 <= 4, merge to [1,5]. Then compare [7,9] with [1,5]: no overlap, move index forward and assign [7,9]. Final intervals: [[1,5],[7,9]].
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Overlapping intervals merged correctly, non-overlapping preserved. [OK]
Quick Trick: Check if current start ≤ last merged end to merge [OK]
Common Mistakes:
MISTAKES
  • Returning original intervals without merging
  • Off-by-one slicing errors
Trap Explanation:
PITFALL
  • Some candidates forget to slice the list after in-place merge, returning extra intervals.
Interviewer Note:
CONTEXT
  • Tests ability to mentally execute in-place merging and understand index management.
Master "Merge Intervals" in Intervals

3 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Intervals Quizzes