0
0
DSA Typescriptprogramming~20 mins

Minimum Number of Platforms in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Platform Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the minimum number of platforms needed?
Given arrival and departure times of trains, what is the minimum number of platforms required so that no train waits?
DSA Typescript
function findPlatform(arr: number[], dep: number[], n: number): number {
  arr.sort((a, b) => a - b);
  dep.sort((a, b) => a - b);
  let platform_needed = 1, result = 1;
  let i = 1, j = 0;
  while (i < n && j < n) {
    if (arr[i] <= dep[j]) {
      platform_needed++;
      i++;
      if (platform_needed > result) result = platform_needed;
    } else {
      platform_needed--;
      j++;
    }
  }
  return result;
}

const arr = [900, 940, 950, 1100, 1500, 1800];
const dep = [910, 1200, 1120, 1130, 1900, 2000];
console.log(findPlatform(arr, dep, arr.length));
A4
B3
C2
D5
Attempts:
2 left
💡 Hint
Sort arrival and departure times separately and compare them to count overlaps.
Predict Output
intermediate
2:00remaining
Output of platform calculation with overlapping trains
What is the output of the following code that calculates minimum platforms?
DSA Typescript
const arrivals = [1000, 1010, 1025, 1030];
const departures = [1015, 1020, 1035, 1040];
function minPlatforms(arr: number[], dep: number[]): number {
  arr.sort((a, b) => a - b);
  dep.sort((a, b) => a - b);
  let platforms = 1, maxPlatforms = 1;
  let i = 1, j = 0;
  while (i < arr.length && j < dep.length) {
    if (arr[i] <= dep[j]) {
      platforms++;
      i++;
      if (platforms > maxPlatforms) maxPlatforms = platforms;
    } else {
      platforms--;
      j++;
    }
  }
  return maxPlatforms;
}
console.log(minPlatforms(arrivals, departures));
A3
B4
C2
D1
Attempts:
2 left
💡 Hint
Count how many trains overlap at the same time.
🧠 Conceptual
advanced
1:30remaining
Why do we sort arrival and departure times separately?
In the minimum number of platforms problem, why is it important to sort arrival and departure times separately before processing?
ATo process events in chronological order and correctly count overlapping trains
BTo group all arrivals before departures for easier counting
CTo ensure trains are processed in the order they appear in input
DTo reduce the time complexity to O(n) without sorting
Attempts:
2 left
💡 Hint
Think about how to track when trains overlap in time.
🔧 Debug
advanced
2:00remaining
Identify the error in this platform calculation code
What error will this code produce when calculating minimum platforms?
DSA Typescript
function findMinPlatforms(arr: number[], dep: number[]): number {
  arr.sort();
  dep.sort();
  let platforms = 1, maxPlatforms = 1;
  let i = 1, j = 0;
  while (i < arr.length && j < dep.length) {
    if (arr[i] < dep[j]) {
      platforms++;
      i++;
      if (platforms > maxPlatforms) maxPlatforms = platforms;
    } else {
      platforms--;
      j++;
    }
  }
  return maxPlatforms;
}

const arrivals = [900, 940, 950, 1100];
const departures = [910, 1200, 1120, 1130];
console.log(findMinPlatforms(arrivals, departures));
AThe sort() method sorts numbers as strings causing incorrect order
BThe comparison operator should be <= instead of <
CThe loop condition should be i <= arr.length
DThe initial platforms count should be 0 instead of 1
Attempts:
2 left
💡 Hint
Check how JavaScript's sort() works on numbers without a compare function.
🚀 Application
expert
3:00remaining
Calculate platforms needed for large input efficiently
You have 1 million trains with arrival and departure times. Which approach is best to find the minimum number of platforms needed efficiently?
ACheck each train against all others to count overlaps in O(n^2) time
BSort trains by arrival time only and count platforms by incrementing for each train
CUse a hash map to count trains at each minute and find max count in O(n) time
DSort arrivals and departures separately and use two pointers to count overlaps in O(n log n) time
Attempts:
2 left
💡 Hint
Consider time complexity and memory usage for large inputs.