Challenge - 5 Problems
Merge Intervals Master
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?
DSA C++
std::vector<std::pair<int,int>> intervals = {{1,3},{2,6},{8,10},{15,18}}; std::sort(intervals.begin(), intervals.end()); std::vector<std::pair<int,int>> merged; for (auto interval : intervals) { if (merged.empty() || merged.back().second < interval.first) { merged.push_back(interval); } else { merged.back().second = std::max(merged.back().second, interval.second); } } for (auto interval : merged) { std::cout << "{" << interval.first << "," << interval.second << "} "; }
Attempts:
2 left
💡 Hint
Look at how intervals are merged when they overlap.
✗ Incorrect
The code sorts intervals and merges overlapping ones by extending the end of the last merged interval.
❓ Predict Output
intermediateResult after merging intervals with nested overlaps
What is the output of this code merging intervals with nested overlaps?
DSA C++
std::vector<std::pair<int,int>> intervals = {{1,4},{4,5},{5,6},{7,8}}; std::sort(intervals.begin(), intervals.end()); std::vector<std::pair<int,int>> merged; for (auto interval : intervals) { if (merged.empty() || merged.back().second < interval.first) { merged.push_back(interval); } else { merged.back().second = std::max(merged.back().second, interval.second); } } for (auto interval : merged) { std::cout << "{" << interval.first << "," << interval.second << "} "; }
Attempts:
2 left
💡 Hint
Check how intervals that touch at edges merge.
✗ Incorrect
Intervals {1,4}, {4,5}, and {5,6} merge into {1,6} because they overlap or touch.
🔧 Debug
advancedIdentify the error in this merge intervals code
What error does this code produce when merging intervals?
DSA C++
std::vector<std::pair<int,int>> intervals = {{1,3},{3,4},{5,7}}; std::vector<std::pair<int,int>> merged; for (auto interval : intervals) { if (merged.empty() || merged.back().second <= interval.first) { merged.push_back(interval); } else { merged.back().second = std::max(merged.back().second, interval.second); } } for (auto interval : merged) { std::cout << "{" << interval.first << "," << interval.second << "} "; }
Attempts:
2 left
💡 Hint
Check the condition that decides when to merge.
✗ Incorrect
The condition uses <= which causes intervals that touch to not merge, so {1,3} and {3,4} remain separate.
❓ Predict Output
advancedOutput of merging intervals with unsorted input
What is the output after merging these unsorted intervals?
DSA C++
std::vector<std::pair<int,int>> intervals = {{5,7},{1,4},{2,6},{8,10}}; std::sort(intervals.begin(), intervals.end()); std::vector<std::pair<int,int>> merged; for (auto interval : intervals) { if (merged.empty() || merged.back().second < interval.first) { merged.push_back(interval); } else { merged.back().second = std::max(merged.back().second, interval.second); } } for (auto interval : merged) { std::cout << "{" << interval.first << "," << interval.second << "} "; }
Attempts:
2 left
💡 Hint
Remember the code sorts intervals before merging.
✗ Incorrect
After sorting, intervals merge into {1,7} and {8,10}.
🧠 Conceptual
expertMinimum number of intervals after merging
Given intervals {{1,5},{2,3},{4,6},{7,8},{8,10},{12,15}}, what is the minimum number of intervals after merging all overlapping ones?
Attempts:
2 left
💡 Hint
Merge intervals that overlap or touch.
✗ Incorrect
Intervals {1,5}, {2,3}, and {4,6} merge into one {1,6}. Intervals {7,8} and {8,10} merge into {7,10}. Interval {12,15} stays separate. Total 3 intervals.
