Data Structures & AlgorithmsHardProduct company OA · Amazon/Microsoft-style OA
Sprinklers at positions p with radius r cover the closed interval [p - r, p + r] of a garden. The sprinklers are (1,2), (4,1), (7,3), (12,2), (15,3), (9,2), and the garden is [0, 18]. What is the minimum number of sprinklers that must be switched on so that the whole garden is covered?
- A. 6
- B. 7
- C. 4
- D. 5
Correct answer: D. 5
Clip each sprinkler's coverage to the garden and sweep from left to right, always choosing among the sprinklers that start no later than the covered point the one reaching furthest. Counting sprinklers that reach the garden's edges misses the middle gaps.
More Coding Interview Problems questions
- Jump game: array a = [2, 3, 1, 1, 4, 2, 1, 3, 1, 2]. Starting at index 0, from index i you may jump forward to any index between i+1 and…
- Packages with weights [6, 2, 8, 4, 5, 3, 7, 1, 9] must be shipped in order using at most 3 days, each day loading a contiguous block of…
- What is the minimum number of swaps of two arbitrary elements needed to sort the array [3, 1, 4, 2] into increasing order?
- Given the numbers 3, 30, 34 and 5, arrange them in some order and concatenate the decimal strings. What is the largest number that can be…
- Problem: Count (possibly overlapping) occurrences of a pattern in a text with KMP. python def kmpcount(t, p): f = [0] len(p); k = 0 for i…
- Problem: Count DISTINCT palindromic substrings by expanding around centres and collecting them in a set. python def distinctpal(s): seen =…