Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to sort the arrival times array.
DSA Typescript
arrivals.sort((a, b) => a [1] b); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' causes incorrect sorting.
Using '*' or '/' in compare function causes runtime errors.
✗ Incorrect
To sort numbers in ascending order, subtract b from a in the compare function.
2fill in blank
mediumComplete the code to increment the platform count when a train arrives before the earliest departure.
DSA Typescript
if (arrivals[i] [1] departures[j]) { platforms++; i++; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>=' causes wrong platform count.
Using '<=' includes equal times incorrectly.
✗ Incorrect
If arrival time is less than departure time, we need a new platform.
3fill in blank
hardFix the error in updating the maximum platforms needed.
DSA Typescript
maxPlatforms = Math.[1](maxPlatforms, platforms); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Math.min causes incorrect minimum platform count.
Using Math.abs or Math.round is unrelated to max calculation.
✗ Incorrect
We use Math.max to keep track of the highest number of platforms needed.
4fill in blank
hardFill both blanks to correctly move pointers when a train departs.
DSA Typescript
else { platforms[1]; j[2]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Increasing platforms on departure causes errors.
Moving pointers in wrong direction breaks logic.
✗ Incorrect
When a train departs, decrease platforms and move departure pointer forward.
5fill in blank
hardFill all three blanks to complete the function that returns minimum platforms needed.
DSA Typescript
function minPlatforms(arrivals: number[], departures: number[]): number {
arrivals.sort((a, b) => a [1] b);
departures.sort((a, b) => a [2] b);
let platforms = 0, maxPlatforms = 0;
let i = 0, j = 0;
while (i < arrivals.length && j < departures.length) {
if (arrivals[i] [3] departures[j]) {
platforms++;
i++;
maxPlatforms = Math.max(maxPlatforms, platforms);
} else {
platforms--;
j++;
}
}
return maxPlatforms;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' in sort causes string concatenation errors.
Using '>' in if condition reverses logic.
✗ Incorrect
Sort both arrays ascending using '-', and compare if arrival < departure to increment platforms.