Reported April 2026
Geminigreedy

Minimum Fence Painting Operations

Reported by candidates from Gemini's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Gemini OA. Under 2s to a working solution.
Founder's read

You're facing Gemini's fence painting problem in April 2026, and it's a greedy optimization disguise. You have n boards of varying heights and a brush of width 1. The catch: you can either paint a single board fully, or paint a horizontal stripe across some consecutive boards at a fixed height. This isn't about brute force. It's about finding the minimum number of strokes to cover everything. StealthCoder will spot the pattern the moment you see the heights array.

The problem

A fence is made of n adjacent vertical boards. Board i has width 1 and height heights[i]. You have a brush of width 1 and may perform only these two types of painting operations: Return the minimum number of operations required to paint the entire fence. Function Description Complete the function minPaintOperations in the editor below. minPaintOperations has the following parameter: Returns The source thread did not provide explicit numeric bounds.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is greedy with a height-sorting insight. Sort the unique heights you need to paint. For each height level, count how many contiguous segments of boards need painting at that level. Each segment costs one operation. Start from the bottom and work up: for height h, only boards with height >= h matter. Paint them in one operation per contiguous run. The pattern emerges fast: this is a sweep-line greedy problem where you minimize segment count at each level. Common pitfall: trying to merge operations across non-contiguous boards. StealthCoder catches that in real time and feeds you the segment-counting approach before you spiral.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Minimum Fence Painting Operations cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder
⏵ The honest play

You've seen the question. Make sure you actually pass Gemini's OA.

Gemini reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Minimum Fence Painting Operations FAQ

Is this really just counting contiguous segments?+

Yes and no. You process heights level by level from bottom to top. At each height, you count runs of boards that need painting at that level. Each run is one operation. The trick is understanding that you can't compress across gaps, so segment count per level is your answer at that level.

Do I need dynamic programming?+

No. This is greedy, not DP. Sort the heights, iterate through each unique height, and count contiguous segments of boards tall enough to need painting at that height. Sum the segment counts. DP will burn time.

What if all boards are the same height?+

One operation. Paint the whole fence in a single horizontal stripe. If heights are all distinct, you may have many more operations because each level might fragment into multiple runs.

How do I prepare for this in 48 hours?+

Understand the two operation types: full board paint and horizontal stripe. Sketch a small example like [1, 3, 2, 3, 1] and trace through level-by-level. The pattern clicks fast once you see the first example worked out.

Will greedy always beat other approaches?+

Yes. Greedy is optimal here because you must cover each (board, height) pair, and a stripe operation covers horizontal runs most efficiently. No overlap or reordering saves operations once you've sorted heights.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Gemini.

OA at Gemini?
Invisible during screen share
Get it