Bird
Raised Fist0

What is the subtle bug?

medium🐞 Bug Identification Q7 of Q15
Intervals - Interval List Intersections
Examine the following code snippet for interval intersections. What is the subtle bug? ```python def intervalIntersection(A, B): i, j = 0, 0 result = [] while i < len(A) and j < len(B): if A[i][1] <= B[j][0]: i += 1 elif B[j][1] < A[i][0]: j += 1 else: start = max(A[i][0], B[j][0]) end = min(A[i][1], B[j][1]) result.append([start, end]) if A[i][1] < B[j][1]: i += 1 else: j += 1 return result ```
ANot handling empty input lists
BNot incrementing both pointers when intervals are equal
CUsing '<=' instead of '<' in the first if condition causes missed intersections at boundary points
DAppending intervals without checking if start <= end
Step-by-Step Solution
Solution:
  1. Step 1: Analyze overlap condition

    Using '<=' in 'if A[i][1] <= B[j][0]' skips intervals that touch at a point, missing valid intersections.
  2. Step 2: Correct condition

    Should use '<' to allow intervals that share a boundary point to be considered overlapping.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Boundary intersections missed due to incorrect comparison [OK]
Quick Trick: Check overlap condition carefully [OK]
Common Mistakes:
MISTAKES
  • Using <= instead of <
  • Missing boundary intersections
Trap Explanation:
PITFALL
  • Candidates often use <= thinking intervals must strictly overlap, missing point intersections.
Interviewer Note:
CONTEXT
  • Tests attention to subtle boundary conditions in interval comparisons.
Master "Interval List Intersections" 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