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]))- A. 2
- B. 4
- C. 3
- 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
- Problem: Return, for each index i, the product of all other elements (no division allowed). Print the list for a = [2, 3, 4, 5]. python a…
- Problem: How many distinct non-empty substrings does the string 'abab' contain? python s = 'abab' print(len({s[i:j] for i in range(len(s))…
- Problem: What is the maximum XOR of any two different elements of the array [3, 10, 5, 25, 2, 8]? python a = [3, 10, 5, 25, 2, 8]…
- Problem: What is the length of the longest contiguous subarray containing at most two distinct values? python from collections import…
- Problem: Remove exactly 3 digits from the number '1432219' to make the smallest possible number, keeping the remaining digits in order…
- Problem: What is the length of the longest palindromic subsequence of 'bbbab'? python def lps(s): n = len(s) dp = [[0] n for in range(n)]…