Bird
Raised Fist0

Given the following code snippet for inserting intervals, what is the output of insert([[1,3],[6,9]], [2,5])?

easy🧾 Code Trace Q3 of Q15
Intervals - Insert Interval
Given the following code snippet for inserting intervals, what is the output of insert([[1,3],[6,9]], [2,5])?
A[[1,5],[6,9]]
B[[1,3],[2,5],[6,9]]
C[[1,3],[6,9],[2,5]]
D[[2,5],[6,9]]
Step-by-Step Solution
Solution:
  1. Step 1: Append and sort intervals

    Intervals become [[1,3],[6,9],[2,5]] then sorted to [[1,3],[2,5],[6,9]].
  2. Step 2: Merge overlapping intervals

    [1,3] and [2,5] overlap, merged to [1,5]; [6,9] remains separate.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Output matches merged intervals after insertion [OK]
Quick Trick: Sort after insertion then merge overlapping intervals [OK]
Common Mistakes:
MISTAKES
  • Forgetting to sort before merging, leading to wrong merges
Trap Explanation:
PITFALL
  • Candidates may forget sorting step, causing incorrect merge order and output.
Interviewer Note:
CONTEXT
  • Tests ability to trace code and understand merging logic.
Master "Insert Interval" 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