FastPrepMinimum Fence Painting Strokes
Problems/Minimum Fence Painting Strokes

Minimum Fence Painting Strokes

Google logoGoogle● HardFULLTIMEPHONE SCREENONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A firsthand report from July 2, 2025, for a Google L4 MLE role, says the onsite problem was the well-known competitive-programming problem Painting Fence. Its limits allow up to 5000 boards, with positive heights up to 10^9. Our version also allows empty fences and boards of zero height. The two stroke directions, contiguous horizontal contact, and permission to repaint match the original problem. Boards are one unit wide and horizontal layers are one unit high, so the brush model is clear. The author's guess about an expected complexity is not treated as a requirement.

A fence consists of adjacent vertical boards, each one unit wide. Board i has height heights[i]. Return the minimum number of brush strokes needed to paint every part of the fence.

A stroke is one of the following:

  • A vertical stroke paints one chosen board from bottom to top, regardless of its height.
  • A horizontal stroke paints one unit-high layer across any consecutive interval of boards. Every board in the interval must reach that layer, so the brush stays in contact throughout.

For this exercise, heights are nonnegative integers and horizontal layers lie between consecutive integer heights. Repainting previously painted areas is allowed, and it still counts as a stroke. A zero-height board has no area to paint and cannot be crossed by a horizontal stroke. An empty fence requires zero strokes.

Function

minPaintStrokes(heights: int[]) → int

Examples

Example 1

heights = [2,2,1,2,1]return = 3

Paint one bottom horizontal layer over all five boards, then two horizontal segments on the second layer. Two strokes cannot cover this shape.

Example 2

heights = [5,5,5]return = 3

Three vertical strokes beat five horizontal layers.

Example 3

heights = [0,0]return = 0

Zero-height boards have no area to paint under the stated convention.

Constraints

  • 0 <= heights.length <= 5000.
  • 0 <= heights[i] <= 10^9.
  • Return an integer count. A valid answer never exceeds the number of boards.

More Google Phone Screen questions

  • Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Periodic-Color Binary Forest RootsPHONE SCREEN · Seen Nov 2026
  • Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
  • Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
  • Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
  • Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Construct a Tree from Level-Order and Inorder TraversalsSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Construct a Tree from Level-Order and Inorder TraversalsAmazon · PHONE SCREEN
  • Trapping Rain WaterMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Rain Kept Between WallsDocuSign · PHONE SCREENDocuSign interview questions
  • Rain Water Held Between BarsAdobe · PHONE SCREENAdobe interview questions
  • Predicate Expression Tree EvaluatorMongoDB · PHONE SCREENMongoDB interview questions
  • Maximum Sum of HeightsVirtu Financial · OAVirtu Financial interview questions
  • Alternating Jumps to the Last IndexExpedia · OAExpedia interview questions
  • Tree Path GCDNewton School · PHONE SCREEN
  • Recursive Display SortFigma · PHONE SCREEN · ONSITE INTERVIEWFigma interview questions

Interview reports that mention this question

  • Google L4 ML Loops and Team Matching: 8 Experiences (Part 2 of 4)

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int minPaintStrokes(int[] heights) {
        // Return the minimum stroke count.
    }
}
Checking account…