Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to sort the arrival times array.
DSA Java
Arrays.[1](arrivals);Attempts:
3 left
💡 Hint
Common Mistakes
Using reverse() instead of sort()
Trying to fill the array instead of sorting
✗ Incorrect
We use Arrays.sort() to sort the arrival times in ascending order.
2fill in blank
mediumComplete the code to check if a train has departed before the next arrives.
DSA Java
if (departures[i] [1] arrivals[j]) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using greater than operator instead of less than or equal
Using equality operator only
✗ Incorrect
We check if the departure time is less than or equal to the next arrival time to free a platform.
3fill in blank
hardFix the error in updating the maximum platforms needed.
DSA Java
maxPlatforms = Math.[1](maxPlatforms, platformsNeeded);Attempts:
3 left
💡 Hint
Common Mistakes
Using Math.min() which gives the smaller value
Using Math.abs() which returns absolute value
✗ Incorrect
We use Math.max() to keep track of the highest number of platforms needed at any time.
4fill in blank
hardFill both blanks to correctly iterate and update platform counts.
DSA Java
while (i < n && j < n) { if (arrivals[i] [1] departures[j]) { platformsNeeded++; maxPlatforms = Math.max(maxPlatforms, platformsNeeded); i++; } else { platformsNeeded[2]; j++; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of <= in comparison
Incrementing platforms when a train departs
✗ Incorrect
We increase platforms when arrival is before or at departure, and decrease when a train departs.
5fill in blank
hardFill all three blanks to create a map of arrival times to platform counts for visualization.
DSA Java
Map<Integer, Integer> platformMap = new HashMap<>(); for (int k = 0; k < n; k++) { platformMap.put(arrivals[k], platformMap.getOrDefault([1], [2]) [3] 1); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong key in getOrDefault
Using subtraction instead of addition
✗ Incorrect
We use arrivals[k] as key, default 0 if not present, and add 1 to count platforms.
