Data Structures & AlgorithmsHardTCS NQT Digital · Product Company SDE
Problem: Find the minimum number of railway platforms with a sweep line over arrival and departure events (a train arriving when another departs at the same minute needs a new platform).
Problem: Find the minimum number of railway platforms with a sweep line over arrival and departure events (a train arriving when another departs at the same minute needs a new platform).
def platforms(arr, dep):
ev = sorted([(t, 1) for t in arr] + [(t, -1) for t in dep], key=lambda e: (e[0], -e[1]))
cur = best = 0
for _, d in ev:
cur += d; best = max(best, cur)
return best
print(platforms([900, 940, 950, 1100, 1500, 1800], [910, 1200, 1120, 1130, 1900, 2000]), platforms([100, 200], [200, 300]))What is printed?- A. 3 2
- B. 3 1
- C. 2 2
- D. 4 2
Correct answer: A. 3 2
Sort events by time; at equal times arrivals (+1) are processed before departures, which implements 'same minute needs a new platform'. Peak overlap in the first schedule is 3 (940–1200, 950–1120, 1100–1130). At minute 200 one train arrives as another leaves → 2; processing departures first would give 1. O(n log n).
More Coding Interview Problems questions
- Problem: Find how many numbers in 1..n have all distinct digits — the digit-DP-free shortcut for n = 10ᵏ uses counting. python def…
- Problem: Find the minimum window length in which every one of k distinct product IDs appears at least once (k = number of distinct values…
- Problem: Find the number of operations to reduce n to 1: if even halve it, else add or subtract 1 (choose optimally). python def steps(n)…
- Problem: Find the minimum number of jumps for a knight to reach a target on an infinite board (BFS). python from collections import deque…
- Problem: Count the inversions of an array with merge sort. python def inv(a): if len(a) < 2: return a, 0 m = len(a) // 2 L, x =…
- Problem: Return the maximum sum of a subarray of size exactly k, and the sum of the best circular subarray (wrapping allowed). python def…