What is the main goal when inserting an interval into a sorted list of intervals?
To add the new interval into the list so that the list remains sorted and all overlapping intervals are merged into one.
Click to reveal answer
beginner
How do you know if two intervals overlap?
Two intervals overlap if the start of one interval is less than or equal to the end of the other interval, and vice versa.
Click to reveal answer
intermediate
What is the first step when inserting a new interval into a sorted list of intervals?
Find all intervals that come before the new interval without overlapping and add them to the result list.
Click to reveal answer
intermediate
What do you do when you find intervals overlapping with the new interval during insertion?
Merge all overlapping intervals by updating the start to the minimum start and the end to the maximum end among them.
Click to reveal answer
intermediate
After merging overlapping intervals, what is the final step in the insertion process?
Add the merged interval to the result list and then add all remaining intervals that come after it.
Click to reveal answer
What condition indicates two intervals [a, b] and [c, d] overlap?
Aa == d
Ba > d
Cb < c
Db >= c and a <= d
✗ Incorrect
Two intervals overlap if the end of the first is at least the start of the second and the start of the first is at most the end of the second.
When inserting a new interval, what do you do with intervals that end before the new interval starts?
AAdd them directly to the result list
BSkip them
CMerge them with the new interval
DRemove them
✗ Incorrect
Intervals that end before the new interval starts do not overlap and can be added directly to the result.
What happens if the new interval overlaps with multiple intervals in the list?
AOnly merge with the first overlapping interval
BMerge all overlapping intervals into one
CIgnore the new interval
DAdd the new interval separately without merging
✗ Incorrect
All overlapping intervals are merged into a single interval to maintain the sorted and non-overlapping property.
After merging the new interval with overlapping intervals, what is the next step?
AAdd the merged interval to the result list
BDiscard the merged interval
CMerge it with the previous interval
DSplit it into smaller intervals
✗ Incorrect
The merged interval is added to the result list to keep the intervals sorted and merged.
What is the time complexity of inserting an interval into a sorted list of intervals?
AO(log n)
BO(n^2)
CO(n)
DO(1)
✗ Incorrect
We may need to scan through all intervals once to find overlaps and insert the new interval, so the time complexity is linear.
Explain step-by-step how to insert a new interval into a sorted list of intervals and merge overlaps.
Think about scanning the list from start to end and handling intervals in three parts.
You got /4 concepts.
Describe how to check if two intervals overlap and how to merge them.
Overlap means intervals share some common range.
You got /3 concepts.
Practice
(1/5)
1. What is the main goal when inserting a new interval into a sorted list of non-overlapping intervals?
easy
A. To merge overlapping intervals and keep the list sorted
B. To add the new interval at the end without merging
C. To remove all intervals that come before the new interval
D. To sort the list again after insertion
Solution
Step 1: Understand the problem context
The list contains sorted, non-overlapping intervals. Inserting a new interval may cause overlaps.
Step 2: Determine the correct action
We must merge any overlapping intervals with the new one to keep the list sorted and non-overlapping.
Final Answer:
To merge overlapping intervals and keep the list sorted -> Option A
Quick Check:
Insert interval = merge and keep sorted [OK]
Hint: Merge overlaps to keep intervals sorted and non-overlapping [OK]
Common Mistakes:
Just appending without merging
Removing intervals incorrectly
Sorting again after insertion
2. Which Go syntax correctly declares a slice of intervals where each interval is a slice of two integers?
easy
A. intervals := [][2]int{{1, 3}, {6, 9}}
B. intervals := [][]int{1, 3, 6, 9}
C. intervals := []int{1, 3, 6, 9}
D. intervals := [][]int{{1, 3}, {6, 9}}
Solution
Step 1: Identify slice of slices syntax
In Go, [][]int means a slice of slices of int, suitable for intervals like [start, end].
Step 2: Check each option
intervals := [][]int{{1, 3}, {6, 9}} correctly uses [][]int with nested slices for intervals. intervals := [][2]int{{1, 3}, {6, 9}} uses array [2]int which is valid but less common. intervals := []int{1, 3, 6, 9} is a flat slice, not intervals. intervals := [][]int{1, 3, 6, 9} is invalid syntax.
Final Answer:
intervals := [][]int{{1, 3}, {6, 9}} -> Option D
Quick Check:
Slice of slices = [][]int{{start,end}} [OK]
Hint: Use [][]int for slice of intervals in Go [OK]
Common Mistakes:
Using flat []int instead of [][]int
Incorrect array vs slice syntax
Missing curly braces for nested slices
3. What is the output of the following Go code snippet after inserting interval [2,5] into intervals [[1,2],[6,9]]?
intervals := [][]int{{1, 2}, {6, 9}}
newInterval := []int{2, 5}
// After insertion and merging
result := [][]int{{1, 5}, {6, 9}}
fmt.Println(result)
medium
A. [[1 2] [6 9] [2 5]]
B. [[1 2] [2 5] [6 9]]
C. [[1 5] [6 9]]
D. [[2 5] [6 9]]
Solution
Step 1: Insert and check overlap
New interval [2,5] overlaps with [1,2] because 2 <= 2. Merge them to [1,5].
Step 2: Keep other intervals
[6,9] does not overlap with [1,5], so it stays as is.
Final Answer:
[[1 5] [6 9]] -> Option C
Quick Check:
Merged intervals = [[1 5] [6 9]] [OK]
Hint: Merge overlapping intervals to combine ranges [OK]
Common Mistakes:
Not merging overlapping intervals
Appending new interval without sorting
Incorrect interval order
4. Given this Go function snippet to insert an interval, what is the bug?
func insert(intervals [][]int, newInterval []int) [][]int {
var result [][]int
for _, interval := range intervals {
if interval[1] < newInterval[0] {
result = append(result, interval)
} else if interval[0] > newInterval[1] {
result = append(result, newInterval)
newInterval = interval
} else {
newInterval[0] = min(newInterval[0], interval[0])
newInterval[1] = max(newInterval[1], interval[1])
}
}
result = append(result, newInterval)
return result
}
medium
A. Appending newInterval multiple times causes duplicates
B. Not updating newInterval correctly during merge
C. Missing return statement
D. Incorrect comparison operators in if conditions
Solution
Step 1: Analyze appending logic
When interval[0] > newInterval[1], newInterval is appended and then replaced by interval. This can cause multiple appends of newInterval.
Step 2: Identify duplicate insertion
Because newInterval is appended inside the loop multiple times, duplicates appear in result.
Final Answer:
Appending newInterval multiple times causes duplicates -> Option A
Quick Check:
Multiple appends = duplicates [OK]
Hint: Append newInterval once after loop to avoid duplicates [OK]
Common Mistakes:
Appending inside loop multiple times
Not updating newInterval boundaries
Forgetting to return result
5. You have intervals [[1,3],[6,9]] and want to insert [2,7]. What is the final list after insertion and merging?
hard
A. [[1,3],[2,7],[6,9]]
B. [[1,9]]
C. [[1,7],[6,9]]
D. [[1,9],[6,9]]
Solution
Step 1: Merge [2,7] with overlapping intervals
[2,7] overlaps with [1,3] and [6,9]. Merge all three intervals into one covering from min start 1 to max end 9.
Step 2: Final merged interval
The merged interval is [1,9], replacing the original two intervals.
Final Answer:
[[1,9]] -> Option B
Quick Check:
Merge all overlapping = [[1,9]] [OK]
Hint: Merge all overlapping intervals into one big interval [OK]
Common Mistakes:
Partial merges leaving overlaps
Appending without merging
Incorrect interval boundaries
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.