Data Structures & AlgorithmsHardAmazon · Microsoft OA
Problem: A van with fixed capacity picks up and drops passengers; trip [n, from, to] carries n passengers from km 'from' to km 'to' (they leave at 'to'). Can it complete all trips? (difference array)
Problem: A van with fixed capacity picks up and drops passengers; trip [n, from, to] carries n passengers from km 'from' to km 'to' (they leave at 'to'). Can it complete all trips? (difference array)
def ok(trips, cap):
d = [0] * 1001
for n, a, b in trips:
d[a] += n; d[b] -= n
cur = 0
for x in d:
cur += x
if cur > cap: return False
return True
t = [[3, 2, 6], [2, 4, 9], [4, 7, 10]]
print(ok(t, 6), ok(t, 5))What is printed?- A. True True
- B. True False
- C. False False
- D. False True
Correct answer: B. True False
Load by km: 3 (km 2–3), 5 (km 4–5), 2 (km 6), 6 (km 7–8), 4 (km 9). The first group leaves at km 6, before the third group boards at 7, so the peak is 6: fine for capacity 6, too many for 5.
More Coding Interview Problems questions
- Problem: Start at index 0 and finish at the last index; from i you may jump to any of i+1..i+k. Maximise the sum of visited values…
- Problem: Move from the top-left to the bottom-right cell of a height grid (4 directions). The effort of a route is the largest absolute…
- Problem: n computers and a list of cables. You may unplug any cable and reconnect it elsewhere. Find the minimum number of moves to…
- Problem: A tree on nodes 1..n received one extra edge. Return the LAST edge in the input whose removal leaves a tree (union-find). python…
- Problem: Replace each word of a sentence by its shortest root from a dictionary (if any root is a prefix). python def replace(roots…
- Problem: Find the shortest palindrome obtainable by adding characters only in FRONT of s, using the KMP failure function of s + '#' +…