Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create an interval class with start and end properties.
DSA C#
public class Interval { public int Start { get; set; } public int End { get; set; } public Interval(int [1], int end) { Start = start; End = end; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using a parameter name that does not match the property, causing confusion.
Forgetting to assign the parameter to the property.
✗ Incorrect
The constructor parameter should be named 'start' to match the assignment to the Start property.
2fill in blank
mediumComplete the code to check if two intervals overlap.
DSA C#
public bool Overlaps(Interval other) {
return this.Start <= other.End && other.Start [1] this.End;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using strict inequality which misses touching intervals.
Reversing the comparison operators.
✗ Incorrect
Two intervals overlap if this.Start <= other.End and other.Start <= this.End.
3fill in blank
hardFix the error in the method that merges two overlapping intervals.
DSA C#
public Interval Merge(Interval other) {
if (!this.Overlaps(other)) {
throw new ArgumentException("Intervals do not overlap");
}
int mergedStart = Math.Min(this.Start, other.Start);
int mergedEnd = Math.Max(this.End, [1]);
return new Interval(mergedStart, mergedEnd);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using the start values instead of end values for merging.
Using the minimum instead of maximum for the end.
✗ Incorrect
To merge intervals, take the maximum end between this.End and other.End.
4fill in blank
hardFill both blanks to create a method that checks if one interval is completely inside another.
DSA C#
public bool IsInside(Interval other) {
return this.Start [1] other.Start && this.End [2] other.End;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using strict inequalities which exclude equal boundaries.
Mixing up the comparison directions.
✗ Incorrect
An interval is inside another if its start is greater or equal and its end is less or equal to the other's.
5fill in blank
hardFill both blanks to create a method that returns a dictionary of intervals with their lengths, only for intervals longer than 5.
DSA C#
public Dictionary<Interval, int> GetLongIntervals(List<Interval> intervals) {
return intervals.Where(i => (i.End - i.Start) [1] 5)
.ToDictionary(i => i, i => i.End [2] i.Start);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in the filter condition.
Using incorrect subtraction or spacing causing syntax errors.
Using the interval object incorrectly as dictionary key.
✗ Incorrect
Filter intervals longer than 5 using '>' and create dictionary with interval as key and length (end - start) as value.
