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?- A. 6
- B. 8
- C. 9
- 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
- Problem: How many distinct subsequences (including the empty one) does 'aba' have? Run the DP below. python def dsub(s): dp = [1] last =…
- Problem: The minimum number of insertions to make 'abcab' a palindrome equals n minus the length of its longest palindromic subsequence…
- Problem: Count pairs (i < j) of a = [1, 2, 3] with a[i] XOR a[j] summed over all pairs. What is the total of the XOR values? Run the code…
- Problem: What is the maximum sum of a path from the top-left to the bottom-right of the grid [[1,3,1],[1,5,1],[4,2,1]] moving only right…
- Problem: How many binary strings of length 5 contain no two consecutive 1s?
- Problem: What is the maximum product of a non-empty contiguous subarray of [2, 3, -2, 4, -1]? Use the code below. python a = [2, 3, -2, 4…