Remove Covered Intervals
Problem Statement
You get a list of intervals [l, r]. No interval appears twice.
Interval [a, b] is covered by interval [c, d] when it sits completely inside it: c ≤ a and b ≤ d.
Remove every interval that is covered by some other interval. Return how many intervals are left.
A list intervals, where intervals[i] = [l, r].
One integer: the number of intervals that are not covered by any other interval.
[3,6] is inside [2,8], so it is removed. [1,4] is not inside [2,8] because it starts earlier. 2 intervals remain.
[2,3] is inside [1,4].
- • 1 ≤ intervals.length ≤ 1000
- • intervals[i].length == 2
- • 0 ≤ l < r ≤ 10^5
- • All intervals are different.
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
For each interval, look for any other interval that contains it
Sort so earlier intervals start first; an interval survives only if it ends past maxEnd
For each interval i, loop over every other interval j. If j starts at or before i and ends at or after i, then i is covered. Count the intervals for which no such j exists.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Intervals translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.