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 Go code after inserting the interval [4, 8] into the sorted list of intervals?
DSA Go
package main import "fmt" func insertInterval(intervals [][]int, newInterval []int) [][]int { var result [][]int i := 0 for i < len(intervals) && intervals[i][1] < newInterval[0] { result = append(result, intervals[i]) i++ } for i < len(intervals) && intervals[i][0] <= newInterval[1] { if intervals[i][0] < newInterval[0] { newInterval[0] = intervals[i][0] } if intervals[i][1] > newInterval[1] { newInterval[1] = intervals[i][1] } i++ } result = append(result, newInterval) for i < len(intervals) { result = append(result, intervals[i]) i++ } return result } func main() { intervals := [][]int{{1, 3}, {6, 9}} newInterval := []int{4, 8} result := insertInterval(intervals, newInterval) fmt.Println(result) }
Attempts:
2 left
💡 Hint
Think about how overlapping intervals are merged after insertion.
✗ Incorrect
The new interval [4,8] overlaps with [6,9], so they merge into [4,9]. The final list is [[1,3],[4,9]].
🧠 Conceptual
intermediateNumber 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
Check which intervals overlap with [4,8] and merge them.
✗ Incorrect
Intervals [3,5],[6,7],[8,10] overlap with [4,8] and merge into [3,10]. The final list is [[1,2],[3,10],[12,16]] with 3 intervals.
🔧 Debug
advancedIdentify the error in interval insertion code
What error will this Go code produce when inserting [2,5] into [[1,3],[6,9]]?
func insertInterval(intervals [][]int, newInterval []int) [][]int {
var result [][]int
i := 0
for i < len(intervals) && intervals[i][1] < newInterval[0] {
result = append(result, intervals[i])
i++
}
for i < len(intervals) && intervals[i][0] <= newInterval[1] {
if intervals[i][0] > newInterval[0] {
newInterval[0] = intervals[i][0]
}
if intervals[i][1] < newInterval[1] {
newInterval[1] = intervals[i][1]
}
i++
}
result = append(result, newInterval)
for i < len(intervals) {
result = append(result, intervals[i])
i++
}
return result
}
Attempts:
2 left
💡 Hint
Check how newInterval boundaries are updated inside the merge loop.
✗ Incorrect
The code incorrectly updates newInterval boundaries by using > and < instead of < and >, causing wrong merged intervals.
📝 Syntax
advancedIdentify syntax error in Go interval insertion snippet
Which option contains a syntax error when trying to append intervals in Go?
DSA Go
var result [][]int intervals := [][]int{{1,3},{6,9}} newInterval := []int{2,5} i := 0 for i < len(intervals) && intervals[i][1] < newInterval[0] { result = append(result, intervals[i]) i++ }
Attempts:
2 left
💡 Hint
Check the syntax of the append function call.
✗ Incorrect
Option C misses a comma between result and intervals[i], causing a syntax error.
🚀 Application
expertFinal intervals after multiple insertions
Starting with an empty list, insert intervals in order: [1,3], [6,9], [2,5], [10,11]. What is the final list of intervals after all insertions and merges?
Attempts:
2 left
💡 Hint
Merge intervals after each insertion carefully.
✗ Incorrect
After inserting [1,3] and [6,9], inserting [2,5] merges with [1,3] to form [1,5]. Then [10,11] is added separately.
