Reported September 2022
ZipRecruiterdynamic programming

Center of the Largest Diagonal X

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 data structure that carries this ZipRecruiter problem is a set of four DP tables, one per diagonal direction. ZipRecruiter candidates reported it in September 2022. You get a 0/1 grid and must find the center of the biggest X made of 1s, with ties broken by row, then column. Brute force looks tempting on a 200 by 200 grid, and it's the trap. If the OA is a day or two out, learn the diagonal run-length trick below. StealthCoder sits invisible on your screen as a safety net if you blank mid-assessment, but you shouldn't need it once this clicks.

The problem

An X of arm length k is centered on a cell containing 1 and contains 1 at every offset d from 0 through k - 1 in all four diagonal directions. Its bounding square has odd side length 2k - 1.
Return the center of an X with maximum arm length, breaking ties by row and then column. A single 1 has arm length one. Return [-1,-1] when the matrix contains no 1.

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

Examples
Example 1
grid = [[1,0,1],[0,1,0],[1,0,1]]
return = [1,1]
The center has arm length two.
Example 2
grid = [[0,0],[0,0]]
return = [-1,-1]
A matrix without one returns the sentinel.

Constraints
1 <= rows, columns <= 200
Every cell is 0 or 1.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Build four matrices: consecutive 1s ending at each cell going up-left, up-right, down-left and down-right. Two passes fill them. A forward pass handles up-left and up-right. A backward pass handles down-left and down-right. For each cell holding a 1, the arm length is the minimum of the four values, since an arm of length k needs k ones in every direction counting the center itself. Track the max, and scan row-major with a strict greater-than so ties resolve to the smallest row, then column automatically. The common pitfall is checking each cell by walking outward, which gives O(n*m*min(n,m)). It passes at 200 but wastes time and invites off-by-one bugs. Another pitfall is forgetting the sentinel [-1,-1] when no 1 exists. Initialize best to 0 and only update when the arm length exceeds it. If your mind goes blank on the DP recurrence in the live OA, StealthCoder can supply it.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Center of the Largest Diagonal X 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. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

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. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Center of the Largest Diagonal X FAQ

What's the trick for the ZipRecruiter largest X problem?+

Precompute diagonal run lengths of consecutive 1s in all four directions with DP. The arm length at a cell is the minimum of the four values. That turns an outward walk per cell into O(1) lookups, so the whole solution is O(rows*columns).

How hard is this one really?+

Medium. The idea is simple once you see the four directional tables. Most of the difficulty is in the indexing, the boundaries, and the tie-break. If you've done any longest-consecutive-ones-in-a-matrix problem, you've seen the core already.

How do I handle the tie-breaking rule?+

Scan the grid in row-major order and update your best only when the new arm length is strictly greater. The first cell reaching the max is then the smallest row, and within that row the smallest column. No extra comparison logic is needed.

What should I return when there's no 1?+

Return [-1,-1]. Start your best arm length at 0 and your answer at [-1,-1]. Any cell with a 1 has arm length at least one, so it overwrites the sentinel. An all-zero grid never triggers an update and the sentinel survives.

Can I prep this in 48 hours?+

Yes. Write the four-direction DP once from scratch and test it on the 3x3 example and the all-zero case. Then check a 1x1 grid and a single row. Those edge cases cover most of the bugs people hit under timed conditions.

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