Data Structures & AlgorithmsHardProduct Company SDE · sweep-line OA

Problem: Trains arrive at times [900, 940, 950, 1100, 1500, 1800] and depart at [910, 1200, 1120, 1130, 1900, 2000]. The two-pointer code below finds the minimum number of platforms. What is printed?

Problem: Trains arrive at times [900, 940, 950, 1100, 1500, 1800] and depart at [910, 1200, 1120, 1130, 1900, 2000]. The two-pointer code below finds the minimum number of platforms. What is printed?
def platforms(arr, dep):
    arr.sort()
    dep.sort()
    i = j = cur = best = 0
    while i < len(arr):
        if arr[i] <= dep[j]:
            cur += 1
            best = max(best, cur)
            i += 1
        else:
            cur -= 1
            j += 1
    return best
print(platforms([900, 940, 950, 1100, 1500, 1800], [910, 1200, 1120, 1130, 1900, 2000]))
  1. A. 2
  2. B. 4
  3. C. 3
  4. D. 6

Correct answer: C. 3

At time 1100 three trains are present (arrived 940, 950 and 1100, departing at 1200, 1120 and 1130), so the sweep peaks at 3. Confusing arrival and departure order gives a different peak.

More Coding Interview Problems questions

Exams that ask this topic