Data Structures & AlgorithmsHardAmazon · Microsoft OA
Problem: Balloons span horizontal intervals [start, end]; an arrow shot at x bursts every balloon with start ≤ x ≤ end. Find the minimum number of arrows.
Problem: Balloons span horizontal intervals [start, end]; an arrow shot at x bursts every balloon with start ≤ x ≤ end. Find the minimum number of arrows.
def arrows(b):
b.sort(key=lambda p: p[1]); cnt, x = 0, float('-inf')
for s, e in b:
if s > x:
cnt += 1; x = e
return cnt
print(arrows([[1,4],[2,3],[3,8],[5,7],[9,10],[8,9]]))What is printed?- A. 3
- B. 2
- C. 4
- D. 5
Correct answer: A. 3
Sorted by end: [2,3], [1,4], [5,7], [3,8], [8,9], [9,10]. Arrow at 3 bursts [2,3], [1,4], [3,8]; [5,7] needs an arrow at 7; [8,9] needs one at 9, which also bursts [9,10] because endpoints count. Total 3.
More Coding Interview Problems questions
- Problem: A knight walks from the top-left to the bottom-right cell moving only right or down; each cell adds its value to his health…
- Problem: Can s3 be formed by interleaving s1 and s2 (keeping each string's own order)? python def inter(s1, s2, s3): if len(s1) + len(s2)…
- Problem: Count palindromic substrings (by position, so equal strings at different places count separately) by expanding around every…
- Problem: Cut a rod of length 7 into integer pieces to maximise total price, where price[i] is the price of a piece of length i + 1…
- Problem: Envelopes [w, h] nest only if both dimensions are strictly smaller. Find the longest nesting chain by sorting and then running…
- Problem: Can the array be split into k subsets with equal sums? (Backtracking over bucket fills.) python def can(a, k): s = sum(a) if s %…