Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create an interval struct with start and end fields.
DSA Go
type Interval struct {
start int
end [1]
}Attempts:
3 left
💡 Hint
Common Mistakes
Using string or bool types for interval boundaries.
Mixing types between start and end fields.
✗ Incorrect
The end field should be of type int to represent the interval's end point.
2fill in blank
mediumComplete the code to check if two intervals overlap.
DSA Go
func overlaps(a, b Interval) bool {
return a.start <= b.end && b.start [1] a.end
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' misses cases where intervals touch at endpoints.
Using '>' or '>=' reverses the logic.
✗ Incorrect
Two intervals overlap if a.start <= b.end and b.start <= a.end.
3fill in blank
hardFix the error in the function that merges two overlapping intervals.
DSA Go
func merge(a, b Interval) Interval {
if !overlaps(a, b) {
return Interval{start: a.start, end: a.end}
}
return Interval{start: min(a.start, b.start), end: [1](a.end, b.end)}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using min instead of max shortens the merged interval.
Using sum or abs causes wrong interval boundaries.
✗ Incorrect
When merging, the end should be the maximum of both ends to cover the full range.
4fill in blank
hardFill both blanks to create a function that merges a list of intervals.
DSA Go
func mergeIntervals(intervals []Interval) []Interval {
if len(intervals) == 0 {
return intervals
}
sort.Slice(intervals, func(i, j int) bool {
return intervals[i].[1] < intervals[j].start
})
merged := []Interval{intervals[0]}
for i := 1; i < len(intervals); i++ {
last := &merged[len(merged)-1]
if overlaps(*last, intervals[i]) {
last.end = max(last.end, intervals[i].[2])
} else {
merged = append(merged, intervals[i])
}
}
return merged
}Attempts:
3 left
💡 Hint
Common Mistakes
Sorting by end instead of start causes incorrect merging order.
Using length or value fields which don't exist.
✗ Incorrect
Sort intervals by start, and when merging, update the end to the max end.
5fill in blank
hardFill all three blanks to create a function that inserts a new interval into a list and merges if needed.
DSA Go
func insertInterval(intervals []Interval, newInterval Interval) []Interval {
result := []Interval{}
i := 0
for i < len(intervals) && intervals[i].end [1] newInterval.start {
result = append(result, intervals[i])
i++
}
for i < len(intervals) && intervals[i].start [2] newInterval.end {
newInterval.start = min(newInterval.start, intervals[i].start)
newInterval.end = [3](newInterval.end, intervals[i].end)
i++
}
result = append(result, newInterval)
for i < len(intervals) {
result = append(result, intervals[i])
i++
}
return result
}Attempts:
3 left
💡 Hint
Common Mistakes
Using <= in first loop includes touching intervals incorrectly.
Using < in second loop misses intervals that touch at the boundary.
Using min instead of max to update newInterval.end.
✗ Incorrect
First loop adds intervals ending before newInterval starts (end < newInterval.start). Second loop merges overlapping intervals (start <= newInterval.end). Use max to update newInterval.end.
