3. What is the minimum number of platforms required for trains with arrival times [900, 940, 950, 1100, 1500, 1800] and departure times [910, 1200, 1120, 1130, 1900, 2000]?
A. platforms should not be decremented when a train departs
B. The loop should run until i < len(arrivals) only
C. The comparison should be arrivals[i] < departures[j], not <=
D. platforms variable should be initialized to 1
Solution
Step 1: Understand platform counting logic
When arrival time equals departure time, we should consider the train has left before the next arrives to avoid extra platform count.
Step 2: Fix comparison operator
Using <= causes increment even when arrival equals departure, inflating platform count. Changing to < fixes this.
Final Answer:
The comparison should be arrivals[i] < departures[j], not <= -> Option C
Quick Check:
Use < to avoid counting simultaneous arrival/departure as overlap [OK]
Hint: Use strict less than (<) to avoid counting equal times as overlap [OK]
Common Mistakes:
Using <= instead of < in time comparison
Incorrect loop termination conditions
Wrong initialization of platform count
5. You have arrival times [900, 940, 950, 1100, 1500, 1800] and departure times [910, 1200, 1120, 1130, 1900, 2000]. How would you modify the algorithm to also return the time intervals when the maximum number of platforms are needed?
hard
A. Track intervals when platform count reaches max during iteration
B. Sort only arrival times and print max arrival time intervals
C. Count total trains and divide by 2 to get intervals
D. Use a queue to store trains and print all arrival times
Solution
Step 1: Track platform count and max count during iteration
While iterating sorted arrivals and departures, keep updating current platform count and record when it reaches a new maximum.
Step 2: Record time intervals for max platforms
Store the start time when max platforms count is reached and end time when it decreases, to get intervals.
Final Answer:
Track intervals when platform count reaches max during iteration -> Option A
Quick Check:
Record max platform count intervals while counting platforms [OK]
Hint: Save start/end times when platform count hits max [OK]
Common Mistakes:
Ignoring departure times when tracking intervals
Only sorting arrivals without departures
Using total trains count instead of overlaps
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.