ProgrammingHardCognizant GenC · TCS NQT

What is printed?

What is printed?
def min_len(a, t):
    best = float('inf')
    s = 0
    l = 0
    for r, x in enumerate(a):
        s += x
        while s >= t:
            best = min(best, r - l + 1)
            s -= a[l]
            l += 1
    return best if best != float('inf') else 0
print(min_len([2, 3, 1, 2, 4, 3], 7), min_len([1, 1], 5))
  1. A. 3 0
  2. B. 2 0
  3. C. 2 -1
  4. D. 4 0

Correct answer: B. 2 0

The shortest window with sum at least 7 is [4, 3], of length 2. When no window reaches the target the function returns 0, not -1.

More Python Programming questions

Exams that ask this topic