Fountain Safety
Updated
Problem statement
You are given an integer array terrain of length n, where terrain[i] is the height at position i. You are also given a strictly increasing integer array fountains containing the positions of the fountains.
A fountain at position f emits water at level terrain[f] in both directions. In either direction, its water crosses consecutive positions whose height is less than or equal to that level and stops before the first position whose height is strictly greater.
- A non-fountain position reached by at least one fountain is watered.
- A fountain does not water its own position.
- A fountain position reached by another fountain is watered only when the other fountain's level is strictly greater than the height at that position. Equal-height water may pass through that fountain position, but does not mark it as watered.
Return an integer array answer of length n, where answer[i] = 1 if position i is watered and answer[i] = 0 otherwise.
Function
getWateredPositions(terrain: int[], fountains: int[]) → int[]Examples
Example 1
terrain = [2, 1, 3, 2, 1, 1]fountains = [0, 3]return = [0, 1, 0, 0, 1, 1]Fountain 0 waters position 1, then stops before the higher position 2. Fountain 3 waters positions 4 and 5. Neither fountain waters its own position.
Example 2
terrain = [1, 2, 1]fountains = [1]return = [1, 0, 1]The fountain at position 1 has level 2, so it waters both neighboring positions of height 1. Its own position remains 0.
Example 3
terrain = [3, 1, 3, 1, 3, 1, 4]fountains = [0, 4]return = [0, 1, 1, 1, 0, 1, 0]Height-3 water crosses the equal-height ordinary position 2 and continues through the equal-height fountain at position 4. Position 4 is not marked because neither equal-height fountain is strictly higher than it. Position 6 blocks the flow because its height is 4.
Constraints
1 <= terrain.length <= 10^50 <= fountains.length <= terrain.length-10^9 <= terrain[i] <= 10^9- Every fountain position is in
[0, terrain.length - 1], andfountainsis strictly increasing.
More Google Onsite Interview questions
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW
- Trace Events from Stable Whole-Stack StatesAnthropic · ONSITE INTERVIEWAnthropic interview questions
- Surviving Balls after Directional CollisionsSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Render a Comment HierarchyAmazon · ONSITE INTERVIEW
- Asteroid CollisionAdobe · ONSITE INTERVIEWAdobe interview questions
- Most Frequent Call Path in Interleaved LogsRoblox · ONSITE INTERVIEWRoblox interview questions
- Asteroid CollisionZscaler · ONSITE INTERVIEWZscaler interview questions
- Asteroid CollisionBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Exclusive Time of FunctionsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Implement Stack Push and Pop OperationsLSEG · ONSITE INTERVIEWLSEG interview questions