Bird
Raised Fist0

After executing addNum(1), addNum(3), and addNum(2) in order, what is the final state of self.intervals?

easy🧾 Code Trace Q12 of Q15
Intervals - Data Stream as Disjoint Intervals
Consider the following Python code implementing the optimal approach for maintaining disjoint intervals. After executing addNum(1), addNum(3), and addNum(2) in order, what is the final state of self.intervals?
A[[1, 1], [3, 3]]
B[[1, 1], [2, 3]]
C[[1, 2], [3, 3]]
D[[1, 3]]
Step-by-Step Solution
Solution:
  1. Step 1: Trace addNum(1)

    Intervals empty, append [1,1]. Intervals = [[1,1]]
  2. Step 2: Trace addNum(3) and addNum(2)

    addNum(3): bisect_left finds i=1, no merges, insert [3,3]. Intervals = [[1,1],[3,3]]. addNum(2): bisect_left finds i=1, left_merge true (1's end +1 == 2), right_merge true (3's start -1 == 2), merge intervals[0] and intervals[1] into [1,3], pop intervals[1]. Intervals = [[1,3]]
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Intervals correctly merged into one [OK]
Quick Trick: Merging occurs when val bridges two intervals [OK]
Common Mistakes:
MISTAKES
  • Forgetting to merge both sides
  • Inserting duplicates
  • Off-by-one errors in merging
Trap Explanation:
PITFALL
  • Candidates often miss the double merge case, leading to separate intervals.
Interviewer Note:
CONTEXT
  • Tests ability to mentally execute balanced tree insertion and merging logic.
Master "Data Stream as Disjoint 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