Bird
Raised Fist0

Given the following code snippet and inputs, what is the output?

easy🧾 Code Trace Q3 of Q15
Intervals - Interval List Intersections
Given the following code snippet and inputs, what is the output? A = [[0,2],[5,10],[13,23],[24,25]] B = [[1,5],[8,12],[15,24],[25,26]] ```python from typing import List def intervalIntersection(A: List[List[int]], B: List[List[int]]) -> List[List[int]]: 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 print(intervalIntersection(A, B)) ```
A[[0,1],[5,8],[13,15],[24,25]]
B[[1,5],[8,12],[15,24],[25,26]]
C[[1,2],[5,10],[13,23],[24,25]]
D[[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
Step-by-Step Solution
Solution:
  1. Step 1: Trace first intersection

    A[0]=[0,2], B[0]=[1,5], overlap is [1,2]. Append [1,2]. A[0][1]
  2. Step 2: Continue tracing intersections

    Next A[1]=[5,10], B[0]=[1,5], overlap [5,5]. Append [5,5]. Now B[0][1]
  3. Step 3: Trace remaining intersections

    Continue similarly: [8,10], [15,23], [24,24], [25,25].
  4. Final Answer:

    Option D -> Option D
  5. Quick Check:

    Output matches expected intersections [OK]
Quick Trick: Trace pointer increments carefully [OK]
Common Mistakes:
MISTAKES
  • Off-by-one in intervals
  • Wrong pointer increment
Trap Explanation:
PITFALL
  • Candidates often miss that intervals touching at a point count as intersection.
Interviewer Note:
CONTEXT
  • Tests ability to mentally execute two pointers intersection code.
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