Challenge - 5 Problems
Interval Insertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after inserting interval into sorted list
What is the output of the following C++ code that inserts an interval into a sorted list of intervals?
DSA C++
std::vector<std::pair<int,int>> insertInterval(std::vector<std::pair<int,int>> intervals, std::pair<int,int> newInterval) { std::vector<std::pair<int,int>> result; int i = 0; while (i < intervals.size() && intervals[i].second < newInterval.first) { result.push_back(intervals[i]); i++; } while (i < intervals.size() && intervals[i].first <= newInterval.second) { newInterval.first = std::min(newInterval.first, intervals[i].first); newInterval.second = std::max(newInterval.second, intervals[i].second); i++; } result.push_back(newInterval); while (i < intervals.size()) { result.push_back(intervals[i]); i++; } return result; } #include <iostream> int main() { std::vector<std::pair<int,int>> intervals = {{1,3}, {6,9}}; std::pair<int,int> newInterval = {2,5}; auto result = insertInterval(intervals, newInterval); for (auto &interval : result) { std::cout << "{" << interval.first << "," << interval.second << "} "; } return 0; }
Attempts:
2 left
💡 Hint
Think about how overlapping intervals merge into one.
✗ Incorrect
The new interval {2,5} overlaps with {1,3}, so they merge into {1,5}. The interval {6,9} remains separate.
❓ Predict Output
intermediateResult after inserting non-overlapping interval
What is the output of this code after inserting a non-overlapping interval into a sorted list?
DSA C++
std::vector<std::pair<int,int>> insertInterval(std::vector<std::pair<int,int>> intervals, std::pair<int,int> newInterval) { std::vector<std::pair<int,int>> result; int i = 0; while (i < intervals.size() && intervals[i].second < newInterval.first) { result.push_back(intervals[i]); i++; } while (i < intervals.size() && intervals[i].first <= newInterval.second) { newInterval.first = std::min(newInterval.first, intervals[i].first); newInterval.second = std::max(newInterval.second, intervals[i].second); i++; } result.push_back(newInterval); while (i < intervals.size()) { result.push_back(intervals[i]); i++; } return result; } #include <iostream> int main() { std::vector<std::pair<int,int>> intervals = {{1,2}, {3,5}, {6,7}, {8,10}, {12,16}}; std::pair<int,int> newInterval = {17,19}; auto result = insertInterval(intervals, newInterval); for (auto &interval : result) { std::cout << "{" << interval.first << "," << interval.second << "} "; } return 0; }
Attempts:
2 left
💡 Hint
New interval does not overlap, so it is added at the end.
✗ Incorrect
Since {17,19} does not overlap with any existing intervals, it is simply appended at the end.
🔧 Debug
advancedIdentify the error in interval insertion code
What error will this code produce when inserting an interval into a sorted list?
DSA C++
std::vector<std::pair<int,int>> insertInterval(std::vector<std::pair<int,int>> intervals, std::pair<int,int> newInterval) { std::vector<std::pair<int,int>> result; int i = 0; while (i < intervals.size() && intervals[i].second < newInterval.first) { result.push_back(intervals[i]); i++; } while (i < intervals.size() && intervals[i].first <= newInterval.second) { newInterval.first = std::min(newInterval.first, intervals[i].first); newInterval.second = std::max(newInterval.second, intervals[i].second); i++; } result.push_back(newInterval); while (i < intervals.size()) { result.push_back(intervals[i]); i++; } return result; } #include <iostream> int main() { std::vector<std::pair<int,int>> intervals = {{1,3}, {6,9}}; std::pair<int,int> newInterval = {2,5}; auto result = insertInterval(intervals, newInterval); for (auto &interval : result) { std::cout << "{" << interval.first << "," << interval.second << "} "; } return 0; }
Attempts:
2 left
💡 Hint
Check the comparison operators in the while loops carefully.
✗ Incorrect
The use of <= and < in the while loops causes the merging condition to be incorrect, so intervals are not merged properly.
🧠 Conceptual
advancedNumber of intervals after insertion and merge
Given a sorted list of intervals {{1,2}, {3,5}, {6,7}, {8,10}, {12,16}} and a new interval {4,8}, how many intervals will be in the list after inserting and merging?
Attempts:
2 left
💡 Hint
Consider which intervals overlap with the new interval.
✗ Incorrect
The new interval {4,8} overlaps with {3,5}, {6,7}, and {8,10}, merging them into one interval {3,10}. The final list is {1,2}, {3,10}, {12,16}, so 3 intervals.
🚀 Application
expertFinal intervals after multiple insertions
Starting with an empty list, insert intervals in this order: {5,7}, {1,3}, {2,6}, {10,12}. What is the final list of intervals after all insertions and merges?
Attempts:
2 left
💡 Hint
Merge intervals as soon as they overlap after each insertion.
✗ Incorrect
Inserting {5,7} then {1,3} results in {1,3}, {5,7}. Inserting {2,6} merges {1,3}, {2,6}, and {5,7} into {1,7}. Finally, {10,12} is separate. Final list: {1,7}, {10,12}.
