Data Structures & AlgorithmsHardProduct Company SDE · sliding window OA

Problem: Count the subarrays of a = [10, 5, 2, 6] whose product is strictly less than k = 100.

Problem: Count the subarrays of a = [10, 5, 2, 6] whose product is strictly less than k = 100.
def cnt(a, k):
    if k <= 1:
        return 0
    p = 1
    l = 0
    ans = 0
    for r, x in enumerate(a):
        p *= x
        while p >= k:
            p //= a[l]
            l += 1
        ans += r - l + 1
    return ans
print(cnt([10, 5, 2, 6], 100))
What is printed?
  1. A. 6
  2. B. 8
  3. C. 9
  4. D. 5

Correct answer: B. 8

The valid subarrays are [10], [5], [2], [6], [10,5], [5,2], [2,6] and [5,2,6], giving 8. The sliding window works because all values are at least 1.

More Coding Interview Problems questions

Exams that ask this topic