Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Minimum Sprinklers to Cover Garden
00:00
5 left

Minimum Sprinklers to Cover Garden

HardPython

Problem

Minimum number of sprinklers to cover the full range (LC 1326 Minimum Number of Taps to Open to Water a Garden)

Asked in the OA stage.

Implement min_sprinklers(n, ranges), where n is the endpoint of a one-dimensional garden interval [0, n], and ranges[i] is the coverage radius of the sprinkler at position i. Return the minimum number of sprinklers needed to cover the full interval, or -1 if coverage is impossible.

Input: integer n and an integer array ranges of length n + 1.

Output: an integer minimum count.

Examples:

  • n = 5, ranges = [3,4,1,1,0,0] returns 1.
  • n = 3, ranges = [0,0,0,0] returns -1.

Constraints: 1 <= n <= 1000; len(ranges) = n + 1; 0 <= ranges[i] <= n.

Constraints

  • 1 <= n <= 1000
  • len(ranges) == n + 1
  • 0 <= ranges[i] <= n
  • Return -1 when complete coverage is impossible

Function Signature

def min_sprinklers(n, ranges):
Interviewer

Your question is Minimum Sprinklers to Cover Garden. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.