Bird
Raised Fist0

Given the following code and input intervals = [[1,4],[3,6],[2,8]], what is the value of count after the loop finishes?

easy🧾 Code Trace Q12 of Q15
Intervals - Remove Covered Intervals
Given the following code and input intervals = [[1,4],[3,6],[2,8]], what is the value of count after the loop finishes?
A2
B1
C3
D0
Step-by-Step Solution
  1. Step 1: Sort intervals by start ascending, end descending

    Sorted intervals: [[1,4],[2,8],[3,6]]
  2. Step 2: Iterate and update count and max_end

    Iteration 1: end=4 > max_end=0 -> count=1, max_end=4 Iteration 2: end=8 > max_end=4 -> count=2, max_end=8 Iteration 3: end=6 ≤ max_end=8 -> count unchanged
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Count increments twice for uncovered intervals [OK]
Quick Trick: Sort then count intervals with strictly greater end [OK]
Common Mistakes:
MISTAKES
  • Misorder intervals after sorting
  • Off-by-one counting in loop
Trap Explanation:
PITFALL
  • Candidates often forget to sort by end descending, causing wrong coverage detection.
Interviewer Note:
CONTEXT
  • Checks ability to trace sorting and counting logic precisely.
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