PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates a candidate's proficiency in algorithm design, specifically greedy strategies and string/array manipulation for coverage and optimization under local adjacency constraints.

  • medium
  • Palo
  • Coding & Algorithms
  • Software Engineer

Find minimum tanks to cover all houses

Company: Palo

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given a string `S` representing a street: - `'H'` = a house - `'-'` = an empty plot where you may place a water tank A house is considered **covered** (able to collect rainwater) if there is a water tank placed **adjacent** to it (either immediately to its left or right). You may place tanks only on empty plots (`'-'`). Your task is to compute the **minimum number of tanks** needed so that **every house** in `S` is covered. If it is impossible to cover all houses, return `-1`. Example: - Input: `S = "-H-HH--"` - One optimal placement is `"-HTHHT-"` (where `'T'` denotes a tank), which uses `2` tanks. Implement a function that returns the minimum number of tanks required (or `-1` if impossible).

Quick Answer: This question evaluates a candidate's proficiency in algorithm design, specifically greedy strategies and string/array manipulation for coverage and optimization under local adjacency constraints.

Place the fewest tanks on empty plots so every house has an adjacent tank, or return -1.

Constraints

  • S contains H and -

Examples

Input: ('-H-HH--',)

Expected Output: 2

Explanation: Prompt-style example.

Input: ('H',)

Expected Output: -1

Explanation: Impossible.

Input: ('H-',)

Expected Output: 1

Explanation: One tank to the right.

Input: ('-H--H',)

Expected Output: 2

Explanation: Greedy right placement.

Hints

  1. Scan left to right; prefer placing a tank to the right to cover possible future houses.
Last updated: Jun 27, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Find Maximum Rectangle in Bar Chart - Palo (medium)
  • Implement an Alert Execution Engine - Palo (medium)
  • Implement three whiteboard coding tasks - Palo (easy)