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?
  1. A. 4 -1 2
  2. B. 4 2 2
  3. C. 4 -1 3
  4. 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

Exams that ask this topic