Bird
Raised Fist0

Consider the following Python function for interval intersections:

easy🧾 Code Trace Q12 of Q15
Intervals - Interval List Intersections
Consider the following Python function for interval intersections:
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

A = [[0,2],[5,10],[13,23],[24,25]]
B = [[1,5],[8,12],[15,24],[25,26]]
print(intervalIntersection(A, B))
What is the final output printed by this code?
A[[1,2],[5,5],[8,10],[15,23],[24,25],[25,26]]
B[[1,2],[5,5],[8,10],[15,23],[24,25],[25,25]]
C[[1,2],[5,5],[8,10],[15,23],[24,24],[25,26]]
D[[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
Step-by-Step Solution
  1. Step 1: Trace first intersection

    Compare [0,2] and [1,5]: overlap is [1,2], add to result.
  2. Step 2: Advance pointers and find all intersections

    Following pointer moves and overlaps yield [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]].
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Matches manual step-by-step intersection results [OK]
Quick Trick: Trace pointer increments carefully to avoid off-by-one errors [OK]
Common Mistakes:
MISTAKES
  • Including intervals beyond intersection
  • Merging adjacent but non-overlapping intervals
  • Off-by-one in pointer increments
Trap Explanation:
PITFALL
  • Candidates often confuse inclusive endpoints or pointer increments causing extra or missing intervals.
Interviewer Note:
CONTEXT
  • Tests ability to mentally execute two-pointer interval 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