Reported November 2021
FlexTradegreedy

Minimum Number of Taps to Water a Garden

Reported by candidates from FlexTrade's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live FlexTrade OA. Under 2s to a working solution.
Founder's read

A garden that runs from 0 through n, n + 1 taps, and a ranges array where tap i covers max(0, i - ranges[i]) to min(n, i + ranges[i]). That's the setup FlexTrade candidates reported in November 2021, and it's Minimum Number of Taps to Water a Garden. It looks like a geometry puzzle, but it's interval covering in disguise, and the greedy version is short. If your OA invite lands in the next day or two, learn the jump-game reframing before anything else. StealthCoder is there as a safety net on the live OA if you freeze, but the idea below is small enough to hold in your head.

The problem

A one-dimensional garden covers the interval from 0 through n. There are n + 1 taps, numbered from 0 through n.
Tap i waters the interval from max(0, i - ranges[i]) through min(n, i + ranges[i]).
Return the minimum number of taps that must be opened to water the entire garden. Return -1 if full coverage is impossible.

Function
minTaps(n: int, ranges: int[]) → int

Examples
Example 1
n = 5
ranges = [3,4,1,1,0,0]
return = 1
Opening tap 1 waters from 0 through 5, covering the whole garden.
Example 2
n = 3
ranges = [0,0,0,0]
return = -1
No tap covers a positive-length interval, so the garden cannot be fully watered.
Example 3
n = 7
ranges = [1,2,1,0,2,1,0,1]
return = 3
Three suitably chosen tap intervals cover every point from 0 through 7; no pair reaches the full interval.

Constraints
1 <= n <= 10000
ranges.length == n + 1
0 <= ranges[i] <= n

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: convert each tap into a jump. For every left endpoint, record the farthest right endpoint any tap starting there can reach. Build an array reach[l] = max(reach[l], r). Now it's Jump Game II. Walk through positions, track the current covered end and the farthest reachable end. When you hit the current end, you must open another tap, so increment the count and move the end to the farthest. If farthest doesn't pass your position, return -1. The common pitfall is forgetting to clamp with max(0,...) and min(n,...), or sorting intervals and getting the tie-breaks wrong. Example 2 with all zeros is the test for the -1 path, since no tap covers positive length. A DP over positions works too but costs more. With n up to 10000, the greedy runs in O(n) and is safe. If you blank mid-assessment, StealthCoder can supply the greedy so you can check your logic against it.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Minimum Number of Taps to Water a Garden 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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

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.

⏵ The honest play

You've seen the question. Make sure you actually pass FlexTrade's OA.

FlexTrade reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Minimum Number of Taps to Water a Garden FAQ

What's the trick to Minimum Number of Taps to Water a Garden?+

Turn every tap into a left-to-right reach. Store the farthest right end for each left end, then run a Jump Game II style greedy. Extend coverage only when you reach the end of the current interval, and count each extension as one tap opened.

How hard is this one really?+

It's labeled hard, but the greedy is about fifteen lines once you see the jump reframing. The difficulty is recognizing it. If you try brute-force subsets or sort and patch intervals, you'll get tangled in edge cases fast.

When should I return -1?+

Return -1 when, at some position, the farthest reachable point doesn't extend past where you currently are. That means a gap exists no tap can bridge. Example 2, where every range is 0, is the clean case to test against.

Can I solve it with dynamic programming instead?+

Yes. Let dp[i] be the fewest taps to cover 0 through i, and update dp[right] from dp[left] for each tap. It's O(n^2) in the worst case, which is risky near n = 10000. The greedy is faster and simpler to write.

How do I prepare for this in 48 hours?+

Do Jump Game II first, then write this problem from scratch using the reach array. Test Example 1, Example 2, and Example 3 by hand. Specifically check clamping at 0 and n, since that's where most wrong answers come from.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with FlexTrade.

OA at FlexTrade?
Invisible during screen share
Get it