Data Structures & AlgorithmsModerateTCS NQT Digital · Product Company SDE
Problem: n ≤ 10⁵ trains arrive and depart at given minutes. Find the minimum number of platforms. Which approach is O(n log n) and correct?
- A. Sort trains by arrival and count overlaps with the next train only
- B. Greedy: assign each train to platform 1 if free, else open a new platform without reuse
- C. Sort arrivals and departures separately and sweep with two pointers, tracking the maximum number of trains present
- D. Binary search the answer with an O(1) check
Correct answer: C. Sort arrivals and departures separately and sweep with two pointers, tracking the maximum number of trains present
A sweep over sorted events counts trains present at every moment; the peak is the answer. Checking only adjacent trains misses three-way overlaps, and never reusing platforms over-counts. A min-heap of departure times is an equivalent O(n log n) method.
More Coding Interview Problems questions
- Problem: Assign cookies of sizes s to children with greed factors g (a child is content if cookie ≥ greed). Maximise content children…
- Problem: A staircase has n steps; you climb 1 or 2 steps at a time. Count the distinct ways to reach the top. cpp long long ways(int n) {…
- Problem: A thief robs houses in a row but cannot rob two adjacent houses. Find the maximum loot. python def rob(a): take = skip = 0 for x…
- Problem: Same robbery, but the houses form a circle (first and last are adjacent). python def line(a): take = skip = 0 for x in a: take…
- Problem: Find the fewest coins that make an amount (−1 if impossible). python def coins(cs, amt): INF = float('inf'); dp = [0] + [INF] amt…
- Problem: Count the number of ways to make an amount from unlimited coins, where order does not matter. python def ways(cs, amt): dp = [1]…