PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Coding & Algorithms/Google

Minimize Fence Painting Operations

Last updated: May 2, 2026

Quick Overview

This question evaluates algorithmic problem-solving and optimization skills, focusing on minimizing painting operations on an array-modeled fence and reasoning about intervals, heights, and operation counts.

  • medium
  • Google
  • Coding & Algorithms
  • Machine Learning Engineer

Minimize Fence Painting Operations

Company: Google

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given a fence made of `n` adjacent vertical boards. Each board has width `1`, and the height of the `i`-th board is `a[i]`. You have a brush of width `1`. In one operation, you may do exactly one of the following: 1. **Vertical stroke:** Paint one entire board from bottom to top. 2. **Horizontal stroke:** Paint one unit-height horizontal segment across a contiguous interval of boards. The stroke must stay on boards that exist at that height. Repainting an already painted area is allowed but unnecessary. Return the minimum number of operations required to paint the entire fence. **Input:** - An integer `n` - An array `a[1..n]` of non-negative integers representing board heights **Output:** - The minimum number of painting operations **Example:** Input: ```text a = [2, 2, 1, 2, 1] ``` Output: ```text 3 ``` Explanation: One optimal strategy is to paint height level `1` horizontally across all five boards, then paint height level `2` horizontally across boards `1..2`, and finally paint height level `2` on board `4`. Total: `3` operations.

Quick Answer: This question evaluates algorithmic problem-solving and optimization skills, focusing on minimizing painting operations on an array-modeled fence and reasoning about intervals, heights, and operation counts.

Related Interview Questions

  • Compute Turnstile Crossing Times - Google (hard)
  • Simulate In-Place Cellular State Updates - Google (hard)
  • Determine Whether a Word Exists in a Graph - Google (medium)
  • Solve Shortest Paths and Rental Allocation - Google (medium)
  • Solve Two Array Optimization Problems - Google (medium)
Google logo
Google
Feb 10, 2026, 12:00 AM
Machine Learning Engineer
Technical Screen
Coding & Algorithms
0
0
Loading...

You are given a fence made of n adjacent vertical boards. Each board has width 1, and the height of the i-th board is a[i].

You have a brush of width 1. In one operation, you may do exactly one of the following:

  1. Vertical stroke: Paint one entire board from bottom to top.
  2. Horizontal stroke: Paint one unit-height horizontal segment across a contiguous interval of boards. The stroke must stay on boards that exist at that height. Repainting an already painted area is allowed but unnecessary.

Return the minimum number of operations required to paint the entire fence.

Input:

  • An integer n
  • An array a[1..n] of non-negative integers representing board heights

Output:

  • The minimum number of painting operations

Example:

Input:

a = [2, 2, 1, 2, 1]

Output:

3

Explanation: One optimal strategy is to paint height level 1 horizontally across all five boards, then paint height level 2 horizontally across boards 1..2, and finally paint height level 2 on board 4. Total: 3 operations.

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Google•More Machine Learning Engineer•Google Machine Learning Engineer•Google Coding & Algorithms•Machine Learning Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ 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.