20. Number of Zero-Filled Subarrays
mediumAsked at RappiCount contiguous subarrays consisting entirely of zeros — Rappi frames this as counting idle windows in a courier's status stream to find under-utilization.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given an integer array nums, return the number of subarrays filled with 0.
Constraints
1 <= nums.length <= 10^5-10^9 <= nums[i] <= 10^9
Examples
Example 1
nums = [1,3,0,0,2,0,0,4]6Example 2
nums = [0,0,0,2,0,0]9Approaches
1. Brute pair check
For every (i,j), check if every element in nums[i..j] is zero.
- Time
- O(n^3)
- Space
- O(1)
let c = 0;
for (let i=0;i<nums.length;i++) for (let j=i;j<nums.length;j++) {
let ok = true;
for (let k=i;k<=j;k++) if (nums[k]!==0) { ok = false; break; }
if (ok) c++;
}
return c;Tradeoff:
2. Run-length math
A run of L zeros contributes L*(L+1)/2 subarrays; sum across runs in one pass.
- Time
- O(n)
- Space
- O(1)
function zeroFilledSubarray(nums) {
let run = 0, total = 0;
for (const n of nums) {
if (n === 0) { run++; total += run; }
else run = 0;
}
return total;
}Tradeoff:
Rappi-specific tips
Rappi will ask you to justify the L*(L+1)/2 identity out loud — they want closed-form reasoning, not just code that happens to produce the right answer.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Practice these live with InterviewChamp.AI
Drill Number of Zero-Filled Subarrays and other Rappi interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →