The "Count Subarrays With Score Less Than K interview question" defines the score of a subarray as the product of its sum and its length. You are given an array of positive integers and a target k. You need to count all contiguous subarrays whose score is strictly less than k. For example, the score of [1, 2, 3] is .
Companies like Meta and Microsoft ask the "Count Subarrays With Score Less Than K coding problem" to evaluate a candidate's ability to apply the Sliding Window technique to a non-standard condition. Because all elements are positive, the score is "monotonic"—as the subarray grows, the score always increases. This property allows for a highly efficient solution.
This problem is solved using the Sliding Window pattern.
left and right pointers.right and update the current_sum.(current_sum * (right - left + 1)) >= k:
arr[left] from current_sum and increment left.right, the number of valid subarrays ending there is right - left + 1. Add this to the total count.Array: [2, 1, 4, 3, 5],
[1], [2, 1]).[4]).
Total count: 4.long in Java/C++, standard integers in Python) for the score and the final count.Practice identifying "Monotonicity" in subarray problems. If adding an element always moves the condition closer to a limit (like exceeding ), then the "Sliding Window interview pattern" is almost certainly the optimal approach.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Maximum Fruits Harvested After at Most K Steps | Hard | Solve | |
| Minimum Size Subarray Sum | Medium | Solve | |
| Max Consecutive Ones III | Medium | Solve | |
| Subarray Product Less Than K | Medium | Solve | |
| Maximum Frequency of an Element After Performing Operations II | Hard | Solve |