Place the fewest food buckets so every hamster in a row has one next to it

Quick Overview

Given a row of hamsters and empty cells, place the fewest food buckets in empty cells so every hamster has a bucket next to it, or report that it cannot be done. The problem tests a left-to-right greedy scan, boundary handling and an exchange argument for preferring the right neighbor.

Place the fewest food buckets so every hamster in a row has one next to it

Company: Grab

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a 0-indexed string `hamsters` where `hamsters[i]` is either: - `'H'`, meaning there is a hamster at index `i`, or - `'.'`, meaning index `i` is empty. You will add some number of food buckets at the empty indices to feed the hamsters. A hamster can be fed if there is at least one food bucket to its left or to its right. More formally, a hamster at index `i` can be fed if you place a food bucket at index `i - 1` and/or at index `i + 1`. Return the minimum number of food buckets you should place at empty indices to feed all the hamsters, or `-1` if it is impossible to feed all of them. ### Function Signature ```python def minimum_buckets(hamsters: str) -> int: ``` ### Rules - A bucket can only go at an index holding `'.'`. - One bucket at index `j` feeds a hamster at `j - 1` and a hamster at `j + 1` at the same time. - A hamster at index 0 has no left neighbor, and a hamster at the last index has no right neighbor. - If the string has no hamsters, the answer is 0. ### Constraints - `1 <= len(hamsters) <= 10^5` - `hamsters[i]` is either `'H'` or `'.'`. ### Examples ```text Input: hamsters = "H..H" Output: 2 ``` The first hamster can only use index 1 and the last can only use index 2, so both buckets are needed. ```text Input: hamsters = ".H.H." Output: 1 ``` A single bucket at index 2 sits next to both hamsters. ```text Input: hamsters = ".HHH." Output: -1 ``` The hamster at index 2 has hamsters on both sides, so no bucket can be placed next to it.

Overview: Given a row of hamsters and empty cells, place the fewest food buckets in empty cells so every hamster has a bucket next to it, or report that it cannot be done. The problem tests a left-to-right greedy scan, boundary handling and an exchange argument for preferring the right neighbor.

Grab logo
Grab
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a 0-indexed string hamsters where hamsters[i] is either:

  • 'H' , meaning there is a hamster at index i , or
  • '.' , meaning index i is empty.

You will add some number of food buckets at the empty indices to feed the hamsters. A hamster can be fed if there is at least one food bucket to its left or to its right. More formally, a hamster at index i can be fed if you place a food bucket at index i - 1 and/or at index i + 1.

Return the minimum number of food buckets you should place at empty indices to feed all the hamsters, or -1 if it is impossible to feed all of them.

Function Signature

def minimum_buckets(hamsters: str) -> int:

Rules

  • A bucket can only go at an index holding '.' .
  • One bucket at index j feeds a hamster at j - 1 and a hamster at j + 1 at the same time.
  • A hamster at index 0 has no left neighbor, and a hamster at the last index has no right neighbor.
  • If the string has no hamsters, the answer is 0.

Constraints

  • 1 <= len(hamsters) <= 10^5
  • hamsters[i] is either 'H' or '.' .

Examples

Input: hamsters = "H..H"
Output: 2

The first hamster can only use index 1 and the last can only use index 2, so both buckets are needed.

Input: hamsters = ".H.H."
Output: 1

A single bucket at index 2 sits next to both hamsters.

Input: hamsters = ".HHH."
Output: -1

The hamster at index 2 has hamsters on both sides, so no bucket can be placed next to it.