← Google Interview Insights

Google·Machine Learning Engineer·Technical Phone Screen·Senior

Senior
Jun 2026

Summary

Google MLE interview with a classic dynamic programming fence painting problem. The question looks deceptively simple until you actually try to formalize the recurrence.

Questions Asked (1)

Q1

You have a fence of n vertical planks, each with a given height. You can paint a plank with a vertical stroke (one full plank, one operation) or a horizontal stroke across consecutive planks at the same height. Find the minimum number of operations to paint the entire fence.

Algorithms & Data Structures
Author's notes

Spent the first few minutes just drawing out the [2,2,1,2,1] example trying to convince myself I understood what a horizontal stroke even meant physically.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Recognize this as a classic dynamic programming problem (similar to 'Strange Printer' or 'Fence Painting'). Define a DP state over intervals and consider the optimal strategy: either paint the first plank vertically and recurse on the rest, or use a horizontal stroke to cover multiple planks of the same height, which can be modeled by splitting at matching heights. Derive the recurrence and then optimize to O(n^2) or O(n^3) depending on the approach.

Pro tip: Start by discussing the brute-force recursive solution and then optimize with memoization; this shows you understand the problem deeply and can improve efficiency. Also, clarify that horizontal strokes can only be applied at the same height across consecutive planks, which is a key constraint.

1. Understand the problem and constraints

Restate the problem: each operation is either a vertical stroke on one plank or a horizontal stroke across consecutive planks at the same height. The goal is to minimize the total number of strokes. Clarify that horizontal strokes can only be applied if all planks in the range have at least that height (i.e., the stroke is at a height ≤ the minimum height of the range).

2. Define the DP state and base cases

Let dp[i][j] be the minimum number of strokes to paint the subarray of planks from index i to j. Base case: dp[i][i] = 1 (one vertical stroke). For a range, the initial upper bound is the length of the range (all vertical strokes).

3. Derive the recurrence relation

Consider the first plank i. We can paint it vertically, which costs 1 + dp[i+1][j]. Alternatively, if there is an index k > i such that height[k] == height[i], we can paint a horizontal stroke covering from i to k (and possibly beyond) at that height, which effectively merges the painting of these planks. The recurrence: dp[i][j] = min(1 + dp[i+1][j], min_{k: height[k]==height[i]} dp[i+1][k-1] + dp[k][j]). This accounts for the horizontal stroke covering i and k, and then solving the subproblems.

4. Implement and optimize

Use memoization or bottom-up DP to compute dp[0][n-1]. The time complexity is O(n^3) in the naive implementation, but can be optimized to O(n^2) by precomputing next occurrence of each height or using a different DP formulation. Discuss potential optimizations and trade-offs.

5. Test with examples and edge cases

Walk through a small example, e.g., heights = [1,2,1], to verify the recurrence. Consider edge cases: all planks same height (answer 1), strictly increasing heights (answer n), and empty input (answer 0).

Key Points to Mention

  • Dynamic programming over intervals (dp[i][j])
  • Base case: single plank requires 1 stroke
  • Recurrence considering vertical stroke on first plank vs. horizontal stroke merging equal heights
  • Time complexity: O(n^3) naive, can be optimized to O(n^2)
  • Space complexity: O(n^2) for DP table
  • Connection to similar problems like 'Strange Printer' or 'Remove Boxes'

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.