Data Structures & AlgorithmsHardICPC regional · Product Company SDE
Problem: For the 'minimum number of platforms needed for a railway station given arrival and departure times' problem, what is the standard efficient approach and its time complexity?
- A. Sort arrivals and departures separately, then use two pointers to simulate overlapping intervals: O(n log n)
- B. Dynamic programming over all subsets of trains: O(2^n)
- C. A single pass without sorting: O(n)
- D. Brute-force check every pair of trains for overlap: O(n^2), and this is optimal
Correct answer: A. Sort arrivals and departures separately, then use two pointers to simulate overlapping intervals: O(n log n)
Sorting arrival and departure times separately and merging them with two pointers (incrementing a platform counter on an arrival, decrementing on a departure) tracks the maximum simultaneous overlap in O(n log n), dominated by the sort.
More Coding Interview Problems questions
- Problem: Which correctly describes why a hash set gives O(1) average-case membership testing, but a balanced BST set gives O(log n)?
- Problem: Which of these correctly explains why BFS (not DFS) is used to find the shortest path in an unweighted graph?
- Problem: What is the time complexity of detecting a cycle in an undirected graph using Union-Find, processing E edges?
- Problem: A recursive solution explores every possible way to partition a string into palindromic substrings (palindrome partitioning)…
- Problem: Which best describes the relationship between the number of valid Binary Search Trees on n distinct keys and the Catalan numbers?
- Problem: What is the time complexity of the standard iterative binary search, and what precondition must hold for it to work correctly?