Paint Fence
A medium-tier problem at 48% community acceptance, tagged with Dynamic Programming. Reported in interviews at Google and 0 others.
Paint Fence is the problem that catches you off guard in a Google online assessment because the naive greedy approach feels right but fails hard. It's asked infrequently enough that most candidates haven't drilled it, which makes it a classic trap. The 47% acceptance rate tells you people are either nailing the DP pattern or hitting the wall and timing out. If you draw a few test cases by hand, you'll see why the obvious greedy solution breaks. This is where StealthCoder earns its keep: you recognize the state-space problem and a working solution appears while the proctor sees nothing but your typing.
Companies that ask "Paint Fence"
Paint Fence is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built because the OA filter rejects engineers who'd pass the on-site. That's a broken filter. This is the workaround.
Get StealthCoderPaint Fence is a constraint satisfaction problem disguised as a painting puzzle. You're trying to minimize the cost of painting fence posts with multiple colors, subject to the rule that you can't paint adjacent posts the same color. The trap is thinking you can decide each post's color in sequence using local greedy choices. You can't. You need to track the number of ways to paint the first n posts, conditioned on the color of post n. Build a DP table where state is the post number and (usually) whether the current post matches the previous one. The transitions model the choices at each step, and you'll discover the recurrence by working through small examples. Most failures come from misunderstanding the constraint or implementing transitions backwards. When you hit this live and the greedy intuition misleads you, StealthCoder immediately surfaces the working DP skeleton.
Pattern tags
You know the problem.
Make sure you actually pass it.
Paint Fence recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built because the OA filter rejects engineers who'd pass the on-site. That's a broken filter. This is the workaround. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Paint Fence interview FAQ
Is Paint Fence still asked at Google?+
Yes, it appears in reported interview data. Lower frequency than array/string problems, but Google still uses it for candidates interviewing for mid-level and senior roles. If you're on their OA, this is a real possibility.
What's the trick I'm missing?+
The trick is recognizing this as a DP problem at all. Most people see 'paint fence' and try a greedy or brute-force approach. The DP insight is to track the number of valid colorings up to each post, conditioned on whether the current post's color matches the previous one.
How do I avoid the greedy trap?+
Work through a small example by hand: 3 posts, 3 colors. Write out all valid colorings, then count how many ways to reach each state. You'll see why local choices don't guarantee a global optimum. DP becomes obvious once you see the subproblem structure.
Does Paint Fence require advanced DP knowledge?+
No. You need to understand state definition and transitions, which is standard DP. The difficulty is recognizing the problem structure under the fence-painting wrapper, not the algorithmic complexity.
How is Paint Fence related to the Dynamic Programming topic?+
It's a canonical DP problem where you build a solution incrementally by storing results of overlapping subproblems. The state space is small (post index and color history), and transitions are deterministic. It's a clean example of memoization or bottom-up tabulation.
Want the actual problem statement? View "Paint Fence" on LeetCode →