Count Subarrays With Median K
A hard-tier problem at 46% community acceptance, tagged with Array, Hash Table, Prefix Sum. Reported in interviews at Salesforce and 2 others.
Count Subarrays With Median K is a hard problem that shows up in assessments for Salesforce, ThoughtSpot, and Confluent. It's not a straightforward counting problem, the median constraint flips how you'd normally approach subarray queries. Most candidates either brute-force every subarray (too slow) or miss the key insight that transforms the problem into a prefix-sum pattern you can solve with a hash table. The acceptance rate sits around 45%, which means it's genuinely hard but not impossible if you see the trick. If this problem hits your live assessment and you blank on the pattern, StealthCoder solves it in seconds, invisible to the proctor.
Companies that ask "Count Subarrays With Median K"
Count Subarrays With Median 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 because the OA filter rejects engineers who'd pass the on-site. That's a broken filter. This is the workaround.
Get StealthCoderThe trick is recognizing that a subarray has median K if and only if you convert the array first: replace each element with -1 if it's less than K, 0 if it equals K, and +1 if it's greater. Then, the median condition becomes a constraint on the prefix sum. You need to count subarrays where the prefix sum follows specific rules tied to K. This is where Hash Table and Prefix Sum come together. Most people get stuck trying to directly compare medians without the conversion. Once you see the transformation, it becomes a classic prefix-sum problem: track prefix sums as you iterate, use a hash table to count valid ranges. The common pitfall is handling the edge cases around what constitutes a valid median when there's an even or odd subarray length. StealthCoder is the hedge if you hit this pattern in your OA and haven't drilled the median-to-prefix-sum conversion before.
Pattern tags
You know the problem.
Make sure you actually pass it.
Count Subarrays With Median 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 because the OA filter rejects engineers who'd pass the on-site. That's a broken filter. This is the workaround. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count Subarrays With Median K interview FAQ
Why is this so much harder than standard subarray problems?+
Median is not a linear property like sum. The transformation step (elements to -1/0/+1 based on K) is non-obvious and not taught in most interview prep. Once you convert, it's prefix-sum mechanics, but the conversion is the hard part. That's why acceptance is only 45%.
Is brute force enough if the array is small?+
No. Brute force is O(n^2) or worse and won't pass for arrays up to size 10^5. You must use the Hash Table and Prefix Sum approach. Even medium-sized inputs force you to optimize.
Do Salesforce, ThoughtSpot, and Confluent really ask this?+
Yes. All three companies have reported this problem in their assessments. It's a solid signal they want candidates who understand non-obvious array transformations and prefix-sum patterns, not just people who can code basic loops.
What's the key insight I should remember?+
Convert the array based on comparison to K, then solve as a prefix-sum problem. The median condition translates to prefix-sum constraints. Hash Table counts matching prefix sums. That's the entire pattern.
How long does a correct solution take to code?+
If you know the trick, 15 to 20 minutes. If you don't see the transformation, you're stuck. Many candidates spend 40+ minutes on failed brute-force attempts, which is why the acceptance rate is low.
Want the actual problem statement? View "Count Subarrays With Median K" on LeetCode →