Data Structures & AlgorithmsVery hardProduct Company SDE · Amazon/Microsoft-style OA
Problem: Camera clips (start, end) cover parts of an event lasting T = 10 minutes; clips may be trimmed. Find the minimum number of clips covering [0, T] (−1 if impossible) – a jump-game style greedy over 'farthest reach'.
Problem: Camera clips (start, end) cover parts of an event lasting T = 10 minutes; clips may be trimmed. Find the minimum number of clips covering [0, T] (−1 if impossible) – a jump-game style greedy over 'farthest reach'.
def min_clips(clips, T):
reach = [0] * (T + 1)
for s, e in clips:
if s <= T: reach[s] = max(reach[s], e)
count = cur_end = far = 0
for i in range(T):
far = max(far, reach[i])
if i == cur_end:
if far <= i: return -1
count += 1; cur_end = far
return count
print(min_clips([(0, 3), (2, 6), (1, 4), (5, 9), (4, 7), (8, 10)], 10), min_clips([(0, 2), (3, 5)], 5), min_clips([(0, 4), (0, 2), (2, 8)], 8))What is printed?- A. 4 -1 2
- B. 4 2 2
- C. 4 -1 3
- D. 3 -1 2
Correct answer: A. 4 -1 2
reach[s] is the farthest end among clips starting at s. When the scan reaches the end of the current coverage, take the clip reaching farthest among those starting so far. First: (0, 3) → best start ≤ 3 reaches 6 → best start ≤ 6 reaches 9 → (8, 10): 4 clips. Second: nothing covers (2, 3) → −1. Third: (0, 4) then (2, 8) → 2. O(T + n).
More Coding Interview Problems questions
- Problem: A broken calculator shows start and supports only 'double' and 'decrement by 1'. Find the minimum operations to reach target…
- Problem: Return the largest number ≤ n whose digits are non-decreasing from left to right. python def mono(n): d = list(str(n)); mark =…
- Problem: A string has '(', ')' and '', where '' may act as '(', ')' or nothing. Is it balanceable? Track the range [lo, hi] of possible…
- Problem: Bags hold balls; an operation splits one bag into two bags (any sizes). With at most ops operations, minimise the largest bag…
- Problem: Tower of Hanoi: move n disks from peg A to peg C using B, recording every move. python moves = [] def hanoi(n, a, b, c): if n ==…
- Problem: Mirror a binary tree (swap every node's children) and print its inorder traversal. Trees are built from a level-order list by…