Count Subarrays With Score Less Than K
A hard-tier problem at 62% community acceptance, tagged with Array, Binary Search, Sliding Window. Reported in interviews at Pinterest and 0 others.
Count Subarrays With Score Less Than K is a hard problem that appears deceptively simple on first read. Pinterest has asked it. The trap is thinking you need brute force to check every possible subarray, which tanks time complexity and fails immediately. The actual trick combines sliding window with prefix sums to score each subarray in O(n log n) or O(n) time, depending on your approach. If you blank on the pattern during your live assessment, StealthCoder surfaces the working solution invisibly so you can paste and move on.
Companies that ask "Count Subarrays With Score Less Than K"
Count Subarrays With Score Less Than K is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an engineer who got tired of watching his cohort grind for six months and still get filtered at the OA stage.
Get StealthCoderThe problem requires you to count subarrays where the sum of elements times the length of the subarray stays below a threshold. Naive nested loops fail. The insight is that as you expand a window rightward, the score monotonically increases, making sliding window viable. For each right pointer position, you shrink the left pointer until the score drops below K, then all subarrays ending at right with left in that valid range count toward the answer. Binary search on prefix sums offers another route. Most candidates miss that score grows predictably with window size, leading them down TLE paths. This is exactly the kind of problem where contest pressure makes you second-guess the right approach. StealthCoder hedges that moment by showing the optimized solution in real time during your screen-share assessment.
Pattern tags
You know the problem.
Make sure you actually pass it.
Count Subarrays With Score Less Than K recycles across companies for a reason. It's hard-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an engineer who got tired of watching his cohort grind for six months and still get filtered at the OA stage. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count Subarrays With Score Less Than K interview FAQ
Is this problem actually asked at Pinterest or just in LeetCode discussions?+
Pinterest has reportedly asked it, though we can't confirm current hiring loop placement. Hard problems like this one appear sporadically, so it's worth drilling even if your target company doesn't list it explicitly.
Why does brute force fail and how do you know to use sliding window?+
Brute force checking all O(n^2) subarrays with multiplication per subarray is O(n^3), which times out. The key insight is that score monotonically increases as the window expands, so you can shrink from the left to maintain validity and count valid ranges in a single pass.
What's the difference between binary search and sliding window approaches here?+
Sliding window is O(n) if you set it up right. Binary search on prefix sums is cleaner to code but O(n log n). Both work. Sliding window is faster and more elegant if you see the monotonicity property upfront.
How do I avoid off-by-one errors when counting subarrays in the valid range?+
When you shrink left and find a valid position, all subarrays from that left to the current right are valid. Add (left_index - left + 1) to your count. Draw a small example with 3-4 elements to lock in the formula before coding.
Is prefix sum necessary or does it just make the code cleaner?+
Prefix sum isn't strictly necessary for sliding window, but it simplifies subarray sum queries to O(1). Without it, you maintain a running sum as you shift pointers, which works fine. Prefix sum is more useful if you go the binary search route.
Want the actual problem statement? View "Count Subarrays With Score Less Than K" on LeetCode →