Bird
Raised Fist0

Given the following code and input intervals = [[1,4],[3,6],[2,8]], what is the returned count?

easy🧾 Code Trace Q3 of Q15
Intervals - Remove Covered Intervals
Given the following code and input intervals = [[1,4],[3,6],[2,8]], what is the returned count?
A1
B3
C2
D0
Step-by-Step Solution
Solution:
  1. Step 1: Sort intervals

    Sorted intervals: [[1,4],[2,8],[3,6]] (start asc, end desc)
  2. Step 2: Iterate and count

    max_end=0; 4>0 -> count=1, max_end=4; 8>4 -> count=2, max_end=8; 6>8? No -> count stays 2
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Two intervals remain uncovered [OK]
Quick Trick: Sort by start asc, end desc; count when end > max_end [OK]
Common Mistakes:
MISTAKES
  • Not sorting by end descending
  • Using >= instead of > in comparison
Trap Explanation:
PITFALL
  • Candidates often misorder intervals or miscount coverage due to sorting errors.
Interviewer Note:
CONTEXT
  • Tests ability to trace code and understand sorting impact.
Master "Remove Covered 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