Challenge - 5 Problems
Interval Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Merging Overlapping Intervals
What is the output of the following C++ code that merges overlapping intervals and prints the result?
DSA C++
#include <iostream> #include <vector> #include <algorithm> using namespace std; struct Interval { int start, end; }; int main() { vector<Interval> intervals = {{1, 3}, {2, 6}, {8, 10}, {15, 18}}; if (intervals.empty()) return 0; sort(intervals.begin(), intervals.end(), [](Interval a, Interval b) { return a.start < b.start; }); vector<Interval> merged; merged.push_back(intervals[0]); for (int i = 1; i < intervals.size(); i++) { if (merged.back().end >= intervals[i].start) { merged.back().end = max(merged.back().end, intervals[i].end); } else { merged.push_back(intervals[i]); } } for (auto &interval : merged) { cout << interval.start << " -> " << interval.end << " -> null\n"; } return 0; }
Attempts:
2 left
💡 Hint
Think about how overlapping intervals are combined into one.
✗ Incorrect
The code merges intervals that overlap by extending the end of the last merged interval. Intervals {1,3} and {2,6} overlap, so they merge into {1,6}. The others remain separate.
🧠 Conceptual
intermediateWhy Are Intervals Important in Algorithms?
Why are interval problems common and important in algorithm challenges?
Attempts:
2 left
💡 Hint
Think about what intervals represent and why overlapping matters.
✗ Incorrect
Intervals represent ranges that can overlap or be disjoint. Handling overlaps requires sorting and merging, which is a common pattern in many problems.
🔧 Debug
advancedIdentify the Bug in Interval Overlap Check
What error does the following code produce when checking if two intervals overlap?
Code snippet:
bool isOverlap(Interval a, Interval b) {
return a.end > b.start && b.end > a.start;
}
Options:
Attempts:
2 left
💡 Hint
Think about intervals that just touch but do not overlap.
✗ Incorrect
The code uses '>' which excludes intervals that touch at endpoints (like end == start). Usually, intervals that touch are considered overlapping or adjacent, so '>=' is preferred.
❓ Predict Output
advancedOutput of Interval Insertion in Sorted List
What is the output of this C++ code that inserts a new interval into a sorted list of non-overlapping intervals and merges if needed?
DSA C++
#include <iostream> #include <vector> #include <algorithm> using namespace std; struct Interval { int start, end; }; int main() { vector<Interval> intervals = {{1, 2}, {3, 5}, {6, 7}, {8, 10}, {12, 16}}; Interval newInterval = {4, 8}; vector<Interval> result; int i = 0; while (i < intervals.size() && intervals[i].end < newInterval.start) { result.push_back(intervals[i++]); } while (i < intervals.size() && intervals[i].start <= newInterval.end) { newInterval.start = min(newInterval.start, intervals[i].start); newInterval.end = max(newInterval.end, intervals[i].end); i++; } result.push_back(newInterval); while (i < intervals.size()) { result.push_back(intervals[i++]); } for (auto &interval : result) { cout << interval.start << " -> " << interval.end << " -> null\n"; } return 0; }
Attempts:
2 left
💡 Hint
Look at how the new interval merges with overlapping intervals.
✗ Incorrect
The new interval {4,8} overlaps with {3,5}, {6,7}, and {8,10}, so they merge into {3,10}. The others remain separate.
🧠 Conceptual
expertWhy Sorting Is Essential in Interval Problems
Why is sorting intervals by their start time a crucial first step in many interval-related algorithms?
Attempts:
2 left
💡 Hint
Think about how processing intervals in order helps in merging.
✗ Incorrect
Sorting by start time arranges intervals so that overlapping intervals appear consecutively, simplifying merging and overlap detection.
