Minimum Sprinklers to Cover an Interval
Reported by candidates from Quince's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Quince reported this one in August 2026, and the input size is the first thing to read. With n up to 10000, trying every subset of sprinklers is dead on arrival, and even an O(n^2) DP gets uncomfortable. This is the classic minimum taps to water a garden problem in disguise. You get n + 1 sprinklers, each covering a range around its position, and you need the fewest that cover [0, n] or -1 if it's impossible. The answer is a greedy sweep, and it fits in about fifteen lines. If you blank during the OA, StealthCoder is the safety net running invisibly on your screen.
The problem
An interval from 0 through n must be completely covered by sprinklers. There are n + 1 sprinklers. Sprinkler i is located at position i. When activated, it covers the continuous interval from i - ranges[i] through i + ranges[i], clipped to the target interval [0, n]. Return the minimum number of sprinklers that must be activated to cover every point in [0, n]. Return -1 if complete coverage is impossible. Function minSprinklers(n: int, ranges: int[]) → int Examples Example 1 n = 5 ranges = [3,4,1,1,0,0] return = 1 Activating the sprinkler at position 1 covers the entire target interval [0, 5], so one sprinkler is sufficient and optimal. Example 2 n = 3 ranges = [0,0,0,0] return = -1 Every sprinkler covers only its own position, leaving gaps between positions. The full interval cannot be covered. Example 3 n = 7 ranges = [1,2,1,0,2,1,0,1] return = 3 Sprinklers at positions 1, 4, and 7 cover [0, 3], [2, 6], and [6, 7]. Together they cover the target interval, and no pair can reach from 0 through 7. Constraints 1 <= n <= 10000 ranges.length = n + 1 0 <= ranges[i] <= n
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to turn each sprinkler into an interval [max(0, i - r), min(n, i + r)], then think of it as a jump game. Build an array where reach[left] holds the furthest right end of any interval starting at left. Now sweep from 0 to n, tracking the current covered end and the furthest reachable end. When you hit the current end and still haven't reached n, you must activate another sprinkler, so bump the count and move the end to the furthest reach. If furthest reach doesn't pass your position, return -1. The common pitfall is forgetting to clip the left side at 0, which causes negative indexes. Another is sorting intervals and doing it in O(n log n) with off-by-one gaps. Example 2 with all zero ranges is your gap test. If the greedy logic slips under pressure, StealthCoder can hand you the working solution in the live OA.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Minimum Sprinklers to Cover an Interval cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as minimum number of taps to open to water a garden. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Quince's OA.
Quince reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Minimum Sprinklers to Cover an Interval FAQ
What's the trick for the Quince minimum sprinklers problem?+
Convert each sprinkler to a clipped interval, then run a greedy jump-game sweep. Store the furthest right end for each left start. Walk the line, and every time you reach the end of current coverage, pick the interval that extends furthest. Count those picks.
How hard is this really?+
It's a hard-tagged problem on paper, but the greedy solution is short. Once you see it as jump game II, it's medium effort. The difficulty is spotting the reduction and handling the -1 case and boundary clipping correctly.
Will brute force or DP pass with n up to 10000?+
Subset brute force is impossible. An O(n^2) DP might squeak by on some inputs but risks timing out. The greedy runs in O(n) after building the reach array, so go with that and skip the DP entirely.
What edge cases should I test?+
Test all zero ranges, which returns -1 as in example 2. Test a single sprinkler covering everything, like example 1. Test sprinklers near position 0 and n where the interval needs clipping. Also test a gap in the middle where no sprinkler bridges.
How do I prepare in 48 hours?+
Write the greedy from memory twice. Trace example 3 by hand to see the three picks. Then write a version that returns -1 on a gap. Also review jump game II since the logic is the same. Don't memorize code, memorize the sweep invariant.