FastPrepMinimum Number of Taps to Water a Garden
Problems/Minimum Number of Taps to Water a Garden

Minimum Number of Taps to Water a Garden

Goldman Sachs logoGoldman Sachs● HardFULLTIMEOA
QuestionFunctionExamplesConstraints

Problem statement

Source note: A Goldman Sachs HackerRank assessment report links the exact Minimum Number of Taps to Open to Water a Garden exercise and says the test cases matched. This practice version is an approximately 99% match.

A one-dimensional garden covers the closed interval [0, n]. Tap i waters [i - ranges[i], i + ranges[i]], clipped to the garden.

Return the minimum number of taps needed to water every point in [0, n]. Return -1 if complete coverage is impossible.

Function

minimumTaps(n: int, ranges: int[]) → int

Examples

Example 1

n = 5ranges = [3,4,1,1,0,0]return = 1

Tap 1 covers the whole interval [0,5].

Example 2

n = 3ranges = [0,0,0,0]return = -1

No tap covers a positive-length interval.

Constraints

  • 1 <= n <= 100000
  • ranges.length == n + 1
  • 0 <= ranges[i] <= n

More Goldman Sachs problems

  • Palindrome CounterOA · Seen Oct 2026
  • Longest Palindromic SubsequenceOA · Seen Oct 2026
  • Best Average Student ScorePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
All Goldman Sachs interview questionsSee Goldman Sachs hiring insights
Saved
public int minimumTaps(int n, int[] ranges) {
  // write your code here
}
Checking account…