Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Minimum Platforms Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Minimum Platforms Needed for Given Train Schedule
What is the output of the following code that calculates the minimum number of platforms needed for the given train arrival and departure times?
DSA Javascript
function findPlatform(arr, dep) {
  arr.sort((a, b) => a - b);
  dep.sort((a, b) => a - b);
  let platform_needed = 1, max_platforms = 1;
  let i = 1, j = 0;
  while (i < arr.length && j < dep.length) {
    if (arr[i] <= dep[j]) {
      platform_needed++;
      i++;
      if (platform_needed > max_platforms) max_platforms = platform_needed;
    } else {
      platform_needed--;
      j++;
    }
  }
  return max_platforms;
}

const arrivals = [900, 940, 950, 1100, 1500, 1800];
const departures = [910, 1200, 1120, 1130, 1900, 2000];
console.log(findPlatform(arrivals, departures));
A2
B4
C3
D5
Attempts:
2 left
💡 Hint
Sort arrival and departure times separately and compare them to find overlaps.
🧠 Conceptual
intermediate
1:30remaining
Understanding the Core Logic of Minimum Platforms
Which of the following best describes the core logic used to find the minimum number of platforms needed for trains arriving and departing?
AAssign one platform per train without considering overlaps.
BCount the maximum number of trains present at the station at the same time by comparing sorted arrival and departure times.
CFind the difference between the earliest arrival and latest departure time.
DCalculate the total number of trains and divide by two to estimate platforms.
Attempts:
2 left
💡 Hint
Think about how overlapping intervals affect platform count.
🔧 Debug
advanced
2:00remaining
Identify the Error in Platform Calculation Code
What error will the following code produce when run, and why?
DSA Javascript
function findPlatform(arr, dep) {
  arr.sort();
  dep.sort();
  let platform_needed = 1, max_platforms = 1;
  let i = 1, j = 0;
  while (i < arr.length && j < dep.length) {
    if (arr[i] <= dep[j]) {
      platform_needed++;
      i++;
      if (platform_needed > max_platforms) max_platforms = platform_needed;
    } else {
      platform_needed--;
      j++;
    }
  }
  return max_platforms;
}

const arrivals = [900, 940, 950, 1100, 1500, 1800];
const departures = [910, 1200, 1120, 1130, 1900, 2000];
console.log(findPlatform(arrivals, departures));
AThe code outputs 1 because sort() sorts numbers as strings, causing wrong order.
BThe code throws a TypeError because of incorrect variable initialization.
CThe code runs correctly and outputs 3.
DThe code outputs 6 because it counts all trains as overlapping.
Attempts:
2 left
💡 Hint
Check how JavaScript's default sort works on numbers.
❓ Predict Output
advanced
2:00remaining
Output of Platform Calculation with Overlapping Trains
What is the output of the following code snippet?
DSA Javascript
function minPlatforms(arr, dep) {
  arr.sort((a, b) => a - b);
  dep.sort((a, b) => a - b);
  let i = 0, j = 0, platform_needed = 0, max_platforms = 0;
  while (i < arr.length && j < dep.length) {
    if (arr[i] < dep[j]) {
      platform_needed++;
      if (platform_needed > max_platforms) max_platforms = platform_needed;
      i++;
    } else {
      platform_needed--;
      j++;
    }
  }
  return max_platforms;
}

const arrivals = [1000, 1010, 1025, 1030, 1040];
const departures = [1015, 1020, 1035, 1045, 1050];
console.log(minPlatforms(arrivals, departures));
A4
B3
C2
D5
Attempts:
2 left
💡 Hint
Count how many trains overlap at the same time.
🧠 Conceptual
expert
1:30remaining
Why Sorting Arrival and Departure Times Separately is Crucial
Why is it important to sort arrival and departure times separately when calculating the minimum number of platforms needed?
ABecause sorting separately ensures that the trains are processed in the order they arrive only.
BBecause sorting together would merge the arrays and lose the distinction between arrivals and departures.
CBecause sorting separately reduces the time complexity from O(n^2) to O(n log n).
DBecause sorting separately allows us to efficiently track the number of trains at the station by comparing the earliest arrival and departure times step-by-step.
Attempts:
2 left
💡 Hint
Think about how two pointers move through sorted arrival and departure lists.

Practice

(1/5)
1. What does the "Minimum Number of Platforms" problem ask you to find given train arrival and departure times?
easy
A. The least number of platforms needed so no train waits
B. The total number of trains arriving in a day
C. The longest time a train stays on a platform
D. The earliest train arrival time

Solution

  1. Step 1: Understand the problem goal

    The problem asks for the minimum platforms so that no train waits due to platform unavailability.
  2. Step 2: Clarify what is being counted

    We count platforms needed simultaneously for overlapping train times, not total trains or times.
  3. Final Answer:

    The least number of platforms needed so no train waits -> Option A
  4. Quick Check:

    Minimum platforms = least needed to avoid waiting [OK]
Hint: Count overlapping trains to find minimum platforms needed [OK]
Common Mistakes:
  • Confusing total trains with platforms needed
  • Thinking about train durations instead of overlaps
  • Ignoring simultaneous arrivals and departures
2. Which of the following JavaScript code snippets correctly sorts an array of train arrival times stored as numbers?
easy
A. arrivals.sort()
B. arrivals.sort((a, b) => a - b)
C. arrivals.sort((a, b) => b - a)
D. arrivals.sort((a, b) => a + b)

Solution

  1. Step 1: Understand JavaScript sort behavior

    By default, sort() converts elements to strings and sorts lexicographically, which is incorrect for numbers.
  2. Step 2: Use numeric comparison function

    Using (a, b) => a - b sorts numbers in ascending order correctly.
  3. Final Answer:

    arrivals.sort((a, b) => a - b) -> Option B
  4. Quick Check:

    Numeric sort uses (a, b) => a - b [OK]
Hint: Use numeric compare (a, b) => a - b to sort numbers [OK]
Common Mistakes:
  • Using sort() without compare function for numbers
  • Using incorrect compare functions like a + b
  • Sorting in descending order when ascending needed
3. What is the minimum number of platforms needed for trains with arrival times [900, 940, 950, 1100, 1500, 1800] and departure times [910, 1200, 1120, 1130, 1900, 2000]?
medium
A. 4
B. 2
C. 1
D. 3

Solution

  1. Step 1: Sort arrival and departure times

    Arrivals: 900, 940, 950, 1100, 1500, 1800; Departures: 910, 1120, 1130, 1200, 1900, 2000.
  2. Step 2: Count platforms needed by comparing arrivals and departures

    At 900 arrival, platform count=1; 940 arrival before 910 departure? No, so platform count=1; 950 arrival before 1120 departure? Yes, platform count=2; 1100 arrival before 1120 departure? Yes, platform count=3; Then departures reduce count as trains leave.
  3. Final Answer:

    3 -> Option D
  4. Quick Check:

    Max overlapping trains = 3 platforms needed [OK]
Hint: Count overlaps by comparing sorted arrivals and departures [OK]
Common Mistakes:
  • Not sorting times before counting
  • Counting total trains instead of overlaps
  • Mixing arrival and departure comparisons
4. Identify the error in this JavaScript snippet for calculating minimum platforms:
let i = 0, j = 0, platforms = 0, maxPlatforms = 0;
arrivals.sort();
departures.sort();
while (i < arrivals.length && j < departures.length) {
  if (arrivals[i] <= departures[j]) {
    platforms++;
    i++;
    if (platforms > maxPlatforms) maxPlatforms = platforms;
  } else {
    platforms--;
    j++;
  }
}
return maxPlatforms;
medium
A. Using <= instead of < in if condition
B. Incrementing i and j in wrong order
C. Sorting arrays without numeric compare function
D. Not initializing maxPlatforms to arrivals.length

Solution

  1. Step 1: Check sorting method

    arrivals.sort() and departures.sort() sort lexicographically by default, causing wrong order for numbers.
  2. Step 2: Correct sorting for numeric arrays

    Use arrivals.sort((a,b) => a - b) and departures.sort((a,b) => a - b) to sort numbers properly.
  3. Final Answer:

    Sorting arrays without numeric compare function -> Option C
  4. Quick Check:

    Numeric sort needs compare function [OK]
Hint: Always use numeric compare in sort for numbers [OK]
Common Mistakes:
  • Sorting numbers as strings causing wrong order
  • Confusing comparison operators in if condition
  • Incorrect loop increments
5. Given arrival times [1000, 1010, 1025, 1030, 1040] and departure times [1020, 1035, 1045, 1050, 1100], what is the minimum number of platforms needed if trains can arrive and depart at the same time without conflict?
hard
A. 3
B. 2
C. 4
D. 1

Solution

  1. Step 1: Sort arrival and departure times

    Arrivals: 1000, 1010, 1025, 1030, 1040; Departures: 1020, 1035, 1045, 1050, 1100.
  2. Step 2: Count platforms considering arrival == departure means no conflict

    Compare arrivals and departures with condition arrivals[i] < departures[j] (strict less than). Counting overlaps: at 1000 arrival platform=1; 1010 arrival platform=2; 1025 arrival platform=3; 1020 departure reduces platform to 2; 1030 arrival platform=3; 1035 departure reduces to 2; 1040 arrival platform=3; then departures reduce count.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    Strict less than counts simultaneous arrival/departure correctly [OK]
Hint: Use strict < to handle same-time arrival and departure [OK]
Common Mistakes:
  • Using <= causing extra platform count
  • Not sorting times before counting
  • Ignoring simultaneous arrival and departure rule
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard