Bird
0
0
DSA Cprogramming~10 mins

Why Intervals Are a Common Problem Pattern in DSA C - Test Your Knowledge

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a struct for an interval with start and end.

DSA C
typedef struct {
    int start;
    int [1];
} Interval;
Drag options to blanks, or click blank then click option'
Avalue
Blength
Cend
Drange
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'length' instead of 'end' for the second member.
2fill in blank
medium

Complete the code to check if two intervals overlap.

DSA C
int isOverlap(Interval a, Interval b) {
    return !(a.end <= b.[1] || b.end <= a.start);
}
Drag options to blanks, or click blank then click option'
Avalue
Bstart
Clength
Drange
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with 'length' or 'value' which are not interval boundaries.
3fill in blank
hard

Fix the error in the function that merges two overlapping intervals.

DSA C
Interval mergeIntervals(Interval a, Interval b) {
    Interval merged;
    merged.start = a.start < b.start ? a.start : b.[1];
    merged.end = a.end > b.end ? a.end : b.end;
    return merged;
}
Drag options to blanks, or click blank then click option'
Astart
Brange
Cvalue
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'end' or 'length' instead of 'start' for the minimum start.
4fill in blank
hard

Fill both blanks to create a function that sorts intervals by their start time.

DSA C
int compareIntervals(const void *a, const void *b) {
    Interval *i1 = (Interval *)[1];
    Interval *i2 = (Interval *)[2];
    return i1->start - i2->start;
}
Drag options to blanks, or click blank then click option'
Aa
Bb
Cstart
Dend
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'start' or 'end' as variable names instead of 'a' and 'b'.
5fill in blank
hard

Fill all three blanks to create a function that merges a list of intervals.

DSA C
int mergeAllIntervals(Interval intervals[], int n, Interval result[]) {
    if (n == 0) return 0;
    qsort(intervals, n, sizeof(Interval), compareIntervals);
    int idx = 0;
    result[idx] = intervals[0];
    for (int i = 1; i < n; i++) {
        if (result[idx].end >= intervals[i].[1]) {
            if (result[idx].end < intervals[i].[2]) {
                result[idx].end = intervals[i].[3];
            }
        } else {
            idx++;
            result[idx] = intervals[i];
        }
    }
    return idx + 1;
}
Drag options to blanks, or click blank then click option'
Astart
Bend
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up start and end in comparisons and assignments.