Challenge - 5 Problems
Minimum Platforms Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat 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 Rust
fn min_platforms(arrivals: &[i32], departures: &[i32]) -> i32 {
let mut arrivals = arrivals.to_vec();
let mut departures = departures.to_vec();
arrivals.sort_unstable();
departures.sort_unstable();
let mut platform_needed = 0;
let mut max_platforms = 0;
let mut i: usize = 0;
let mut j: usize = 0;
while i < arrivals.len() && j < departures.len() {
if arrivals[i] <= departures[j] {
platform_needed += 1;
max_platforms = max_platforms.max(platform_needed);
i += 1;
} else {
platform_needed -= 1;
j += 1;
}
}
max_platforms
}
fn main() {
let arrivals = vec![900, 940, 950, 1100, 1500, 1800];
let departures = vec![910, 1200, 1120, 1130, 1900, 2000];
println!("{}", min_platforms(&arrivals, &departures));
}Attempts:
2 left
💡 Hint
Sort arrival and departure times and compare them to find overlaps.
✗ Incorrect
The code sorts arrival and departure times, then uses two pointers to count how many trains are at the station simultaneously. The maximum count is the minimum number of platforms needed.
❓ Predict Output
intermediateOutput of platform calculation with overlapping trains
What will be the output of the following Rust code that calculates minimum platforms?
DSA Rust
fn min_platforms(arrivals: &[i32], departures: &[i32]) -> i32 {
let mut arrivals = arrivals.to_vec();
let mut departures = departures.to_vec();
arrivals.sort_unstable();
departures.sort_unstable();
let mut platform_needed = 0;
let mut max_platforms = 0;
let mut i: usize = 0;
let mut j: usize = 0;
while i < arrivals.len() && j < departures.len() {
if arrivals[i] < departures[j] {
platform_needed += 1;
max_platforms = max_platforms.max(platform_needed);
i += 1;
} else {
platform_needed -= 1;
j += 1;
}
}
max_platforms
}
fn main() {
let arrivals = vec![1000, 1010, 1025, 1030];
let departures = vec![1015, 1020, 1035, 1040];
println!("{}", min_platforms(&arrivals, &departures));
}Attempts:
2 left
💡 Hint
Check how many trains overlap at the same time.
✗ Incorrect
The maximum number of trains overlapping is 2, so 2 platforms are needed.
🔧 Debug
advancedWhy does this Rust code panic at runtime?
The following Rust code panics with an index out of bounds error. Why does this happen?
DSA Rust
fn min_platforms(arrivals: &[i32], departures: &[i32]) -> i32 {
let mut arrivals = arrivals.to_vec();
let mut departures = departures.to_vec();
arrivals.sort_unstable();
departures.sort_unstable();
let mut platform_needed = 0;
let mut max_platforms = 0;
let mut i: usize = 0;
let mut j: usize = 0;
while i <= arrivals.len() && j <= departures.len() {
if arrivals[i] <= departures[j] {
platform_needed += 1;
max_platforms = max_platforms.max(platform_needed);
i += 1;
} else {
platform_needed -= 1;
j += 1;
}
}
max_platforms
}
fn main() {
let arrivals = vec![900, 940, 950];
let departures = vec![910, 1200, 1120];
println!("{}", min_platforms(&arrivals, &departures));
}Attempts:
2 left
💡 Hint
Check the loop condition and how indices are used inside the loop.
✗ Incorrect
The loop condition uses <= which allows i or j to become equal to the length of the arrays, causing out of bounds access when indexing.
🧠 Conceptual
advancedWhy sorting arrival and departure times is essential?
Why do we sort the arrival and departure times separately when calculating the minimum number of platforms?
Attempts:
2 left
💡 Hint
Think about how sorting helps in comparing arrival and departure events.
✗ Incorrect
Sorting allows us to process arrival and departure events in order, so we can track how many trains are at the station at any time.
🚀 Application
expertCalculate minimum platforms for large input efficiently
You have 1 million trains with random arrival and departure times. Which approach is best to calculate minimum platforms efficiently?
Attempts:
2 left
💡 Hint
Consider time complexity and scalability for large inputs.
✗ Incorrect
Sorting arrivals and departures and using two pointers is efficient and scalable for large inputs, running in O(n log n) time.
