Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to sort the arrival times array.
DSA Javascript
arrivals.sort((a, b) => a [1] b);Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' in the comparator.
Using '*' which does not work for sorting.
✗ Incorrect
The sort function needs a comparator that returns a positive number when a > b, so 'a + b' is incorrect. The correct comparator is 'a - b' to sort ascending.
2fill in blank
mediumComplete the code to increment the platform count when a train arrives before the earliest departure.
DSA Javascript
if (arrivals[i] < departures[j]) { platforms_needed [1]; i++; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '--' which decreases the count.
Using '-=' which subtracts.
✗ Incorrect
When a train arrives before the earliest departure, we need one more platform, so we increment platforms_needed by 1 using '++'.
3fill in blank
hardFix the error in updating the maximum platforms needed.
DSA Javascript
max_platforms = Math.max(max_platforms, [1]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using platforms_needed++ which changes the value unexpectedly.
Using platforms_needed + 1 which is incorrect here.
✗ Incorrect
We should compare max_platforms with the current platforms_needed, not with platforms_needed++ or other expressions that change the value.
4fill in blank
hardFill both blanks to correctly move pointers when a train departs before the next arrival.
DSA Javascript
else { platforms_needed [1]; j [2]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Increasing platforms_needed instead of decreasing.
Moving the wrong pointer.
✗ Incorrect
When a train departs before the next arrival, we reduce platforms_needed by 1 using '--' and move the departure pointer forward using '++'.
5fill in blank
hardFill all three blanks to complete the function that calculates minimum platforms.
DSA Javascript
function minPlatforms(arrivals, departures) {
arrivals.sort((a, b) => a [1] b);
departures.sort((a, b) => a [2] b);
let platforms_needed = 0, max_platforms = 0;
let i = 0, j = 0;
while (i < arrivals.length && j < departures.length) {
if (arrivals[i] < departures[j]) {
platforms_needed [3];
i++;
} else {
platforms_needed--;
j++;
}
if (platforms_needed > max_platforms) max_platforms = platforms_needed;
}
return max_platforms;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' or '*' in sort comparator.
Not incrementing platforms_needed correctly.
✗ Incorrect
We sort both arrays ascending using 'a - b'. We increment platforms_needed by 1 when a train arrives before the earliest departure using '++'.
