Challenge - 5 Problems
Interval Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Merging Overlapping Intervals
What is the output of the following Python code that merges overlapping intervals?
DSA Python
def merge_intervals(intervals): intervals.sort(key=lambda x: x[0]) merged = [] for interval in intervals: if not merged or merged[-1][1] < interval[0]: merged.append(interval) else: merged[-1][1] = max(merged[-1][1], interval[1]) return merged intervals = [[1,3],[2,6],[8,10],[15,18]] print(merge_intervals(intervals))
Attempts:
2 left
💡 Hint
Think about how overlapping intervals are combined into one.
✗ Incorrect
The code sorts intervals by start time, then merges overlapping ones by updating the end time of the last merged interval.
🧠 Conceptual
intermediate1:30remaining
Why Sorting is Important in Interval Problems
Why is sorting intervals by their start time a common first step in interval problems?
Attempts:
2 left
💡 Hint
Think about how intervals relate to each other when ordered by start time.
✗ Incorrect
Sorting by start time allows us to check each interval against the previous one to detect overlaps efficiently.
🔧 Debug
advanced1:30remaining
Identify the Error in Interval Overlap Check
What error does this code raise when checking if two intervals overlap?
DSA Python
def is_overlapping(a, b): return a[1] > b[0] and b[1] > a[0] print(is_overlapping([1,3], [3,5]))
Attempts:
2 left
💡 Hint
Check how the code treats intervals that just touch at the edges.
✗ Incorrect
The code returns False for intervals that touch at the boundary (like [1,3] and [3,5]) because it uses > instead of >=, so touching intervals are not considered overlapping.
🚀 Application
advanced2:00remaining
Find Maximum Number of Overlapping Intervals
Given intervals, what is the maximum number of intervals overlapping at any point?
DSA Python
intervals = [[1,4],[2,5],[7,9],[3,6]] # What is the maximum overlap count?
Attempts:
2 left
💡 Hint
Visualize intervals on a timeline and count overlaps at each point.
✗ Incorrect
Intervals [1,4], [2,5], and [3,6] overlap between 3 and 4, so maximum overlap is 3.
❓ Predict Output
expert2:30remaining
Output of Interval Scheduling Maximum Set
What is the output of the code that finds the maximum number of non-overlapping intervals?
DSA Python
def max_non_overlapping(intervals): intervals.sort(key=lambda x: x[1]) count = 0 end = float('-inf') for interval in intervals: if interval[0] >= end: count += 1 end = interval[1] return count intervals = [[1,2],[2,3],[3,4],[1,3]] print(max_non_overlapping(intervals))
Attempts:
2 left
💡 Hint
Think about choosing intervals that end earliest to maximize count.
✗ Incorrect
The code selects intervals greedily by earliest end time, resulting in 3 non-overlapping intervals: [1,2], [2,3], and [3,4].