Bird
Raised Fist0

What is the time complexity of the line sweep algorithm for counting intervals containing each point, given n intervals and m points? Assume sorting uses a comparison-based sort.

medium🪤 Complexity Trap Q13 of Q15
Intervals - Count of Intervals Containing Each Point
What is the time complexity of the line sweep algorithm for counting intervals containing each point, given n intervals and m points? Assume sorting uses a comparison-based sort.
AO(n * m)
BO(n + m)
CO(n log n + m log m)
DO((n + m) log (n + m))
Step-by-Step Solution
  1. Step 1: Identify main operations

    The algorithm creates 2n interval events and m point events, total O(n + m) events.
  2. Step 2: Sorting events dominates time

    Sorting O(n + m) events takes O((n + m) log (n + m)) time. The sweep itself is O(n + m).
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Sorting all events is the bottleneck, so complexity is O((n+m) log (n+m)) [OK]
Quick Trick: Sorting all events dominates time complexity [OK]
Common Mistakes:
MISTAKES
  • Assuming O(n*m) from brute force
  • Splitting sorting into separate sorts for intervals and points
  • Ignoring sorting cost
Trap Explanation:
PITFALL
  • Option C looks plausible but sorting all events together is more efficient than separate sorts.
Interviewer Note:
CONTEXT
  • Checks if candidate understands event sorting cost and overall complexity.
Master "Count of Intervals Containing Each Point" 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