Reported October 2024
ZipRecruitermatrix

Minimum Changes to Draw a Y

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

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

The ZipRecruiter OA reported in October 2024 dresses up a counting problem as a drawing problem. Strip the Y shape away and you're left with two buckets of cells and three possible values. You split the grid into Y cells and non-Y cells, count the 0s, 1s and 2s in each, then try every pair of distinct values. That's six combinations, no more. If you blank on the geometry under pressure, StealthCoder runs invisibly during the live assessment and can hand you the structure. The real work is just classifying cells correctly.

The problem

grid is an odd n by n matrix containing only 0, 1, and 2. The Y consists of both upper diagonals through the center and the vertical cells below the center.
Change the fewest cells so all Y cells share one value and all non-Y cells share a different value. Return the minimum changes.

Function
minimumYChanges(grid: int[][]) → int

Examples
Example 1
grid = [[1,2,2],[1,1,0],[0,1,0]]
return = 3
Trying all distinct Y and background values yields a minimum of three changes.
Example 2
grid = [[0,0,0],[0,0,0],[0,0,0]]
return = 4
Use 0 for the five Y cells and recolor the four non-Y cells.

Constraints
3 <= n <= 501
n is odd.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is that you never simulate changes. Make one pass over the grid. For each cell, decide if it's on the Y: the main diagonal (r == c) and anti-diagonal (r + c == n-1) for the rows in the upper half including the center, plus the center column (c == n/2) for rows from the center down. Increment yCount[value] or otherCount[value]. Then for every pair (a, b) with a != b, cost is totalY - yCount[a] + totalOther - otherCount[b]. Take the minimum. The common pitfall is the center cell, which is counted once, and getting the row bound wrong so the diagonals keep going below the center. Another miss is allowing a == b, which the problem forbids. Example 2 shows it: all zeros still costs 4 because the background can't also be 0. If the indexing falls apart mid-OA, StealthCoder is the hedge that gets you unstuck. Runtime is O(n^2) with O(1) extra space.

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 Changes to Draw a Y 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

Related leaked OAs

⏵ The honest play

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

ZipRecruiter 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 Changes to Draw a Y FAQ

What's the trick in Minimum Changes to Draw a Y?+

Don't simulate edits. Count how many 0s, 1s and 2s sit inside the Y and outside it. Then try all six ordered pairs of distinct values for Y and background. Cost for a pair is the Y cells not equal to the Y value plus the outside cells not equal to the background value.

How do I identify the Y cells correctly?+

For a cell (r, c) in an n by n grid, it's on the Y if r <= n/2 and (r == c or r + c == n-1), or if r >= n/2 and c == n/2. Using integer division, the center cell satisfies all of these, so it's counted once with no special case.

Can the Y value and background value be the same?+

No. The problem says the non-Y cells must share a different value from the Y cells. That's why Example 2, an all-zero grid, returns 4 and not 0. You have to recolor the four background cells to something else.

How hard is this really for an OA?+

Easy on algorithm, easy to fumble on indexing. There's no dynamic programming or graph work. With n up to 501 a single O(n^2) pass is plenty. Most lost points come from off-by-one errors at the center row or from double counting the center.

How should I prepare for this in 48 hours?+

Write the solution once from scratch. Build the Y membership check, fill two count arrays of size 3, and loop over the six value pairs. Test it on both examples by hand, especially the all-zero grid. That covers every part of the problem and takes under an hour.

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

OA at ZipRecruiter?
Invisible during screen share
Get it