Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the method that inserts an interval into a sorted list.
DSA Java
public List<Interval> [1](List<Interval> intervals, Interval newInterval) { }Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that does not clearly indicate insertion.
Choosing a method name that implies adding but not necessarily inserting in order.
✗ Incorrect
The method name is 'insertInterval' as per the problem statement.
2fill in blank
mediumComplete the code to initialize the result list that will hold merged intervals.
DSA Java
List<Interval> result = new [1]<>();Attempts:
3 left
💡 Hint
Common Mistakes
Using a Set which does not maintain order or allows duplicates.
Using LinkedList unnecessarily when ArrayList suffices.
✗ Incorrect
ArrayList is the appropriate list implementation to hold intervals in order.
3fill in blank
hardFix the error in the condition to check if the current interval ends before the new interval starts.
DSA Java
if (intervals.get(i).end [1] newInterval.start) { result.add(intervals.get(i)); i++; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>=' which includes touching intervals as non-overlapping incorrectly.
Using '>' which misses intervals that end exactly at newInterval.start.
✗ Incorrect
The condition should check if the current interval ends before the new interval starts, so '<' is correct.
4fill in blank
hardFill both blanks to merge overlapping intervals correctly.
DSA Java
newInterval.start = Math.min(newInterval.start, intervals.get(i).[1]); newInterval.end = Math.max(newInterval.end, intervals.get(i).[2]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect fields like length or value which do not exist.
Swapping start and end in min/max functions.
✗ Incorrect
To merge intervals, update newInterval start to min of starts and end to max of ends.
5fill in blank
hardFill all three blanks to add the merged interval and remaining intervals to the result list.
DSA Java
result.[1](newInterval); while (i [2] intervals.size()) { result.[3](intervals.get(i)); i++; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using remove instead of add which deletes elements.
Using '>=' instead of '<' causing loop to skip or error.
✗ Incorrect
Use add to insert intervals and '<' to loop while i is less than intervals size.
