Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to append the new interval to the result list.
DSA Go
result := [][]int{}
for i := 0; i < len(intervals); i++ {
if intervals[i][1] < newInterval[0] {
result = append(result, intervals[i])
} else if intervals[i][0] > newInterval[1] {
result = append(result, newInterval)
newInterval = intervals[i]
} else {
newInterval[0] = min(intervals[i][0], newInterval[0])
newInterval[1] = max(intervals[i][1], newInterval[1])
}
}
result = append(result, [1])
Attempts:
3 left
💡 Hint
Common Mistakes
Appending the wrong interval like the last original interval instead of the merged one.
Forgetting to append the newInterval at the end.
✗ Incorrect
After merging or skipping intervals, the last newInterval must be appended to the result list.
2fill in blank
mediumComplete the code to check if the current interval is completely before the new interval.
DSA Go
for i := 0; i < len(intervals); i++ { if intervals[i][1] [1] newInterval[0] { result = append(result, intervals[i]) } else if intervals[i][0] > newInterval[1] { // merging logic } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using > or >= which would miss intervals that end exactly at newInterval start.
Using < which excludes intervals that end exactly at newInterval start.
✗ Incorrect
If the end of the current interval is less than or equal to the start of newInterval, it means it is completely before and does not overlap.
3fill in blank
hardFix the error in the merging logic to update the newInterval boundaries correctly.
DSA Go
if intervals[i][0] <= newInterval[1] && intervals[i][1] >= newInterval[0] { newInterval[0] = [1](intervals[i][0], newInterval[0]) newInterval[1] = [2](intervals[i][1], newInterval[1]) }
Attempts:
3 left
💡 Hint
Common Mistakes
Using max for start or min for end which breaks the interval boundaries.
Using sum or abs which are not relevant here.
✗ Incorrect
To merge intervals, the start is the minimum of both starts, and the end is the maximum of both ends.
4fill in blank
hardFill both blanks to complete the helper functions for min and max.
DSA Go
func min(a, b int) int { if a [1] b { return a } return b } func max(a, b int) int { if a [2] b { return a } return b }
Attempts:
3 left
💡 Hint
Common Mistakes
Using >= or <= which can cause incorrect returns when values are equal.
Swapping the operators between min and max.
✗ Incorrect
min returns a if a < b else b; max returns a if a > b else b.
5fill in blank
hardFill all three blanks to complete the main function signature and return statement.
DSA Go
func insert([1] [][]int, [2] []int) [][]int { // implementation return [3] }
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names in the signature or return statement.
Returning the wrong variable instead of the result list.
✗ Incorrect
The function takes intervals and newInterval as inputs and returns the result list.
