Count Nearly Regular Matrix Crosses
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
A hash map per row and per column is what this ZipRecruiter OA, reported in September 2024, is really about. The title says matrix and the word cross sounds like grid traversal, but nothing here needs a search. You count, over every row-column pair, whether the cross cells outside the intersection all share one value. Grids cap at 50 by 50, so even a plain scan passes. The risk is misreading the rules and miscounting the intersection or the 1x1 case. If your head goes blank mid-assessment, StealthCoder is a quiet safety net running on your screen. Read the rules below first and you probably won't need it.
The problem
A cross is one complete row and one complete column of a rectangular integer matrix. A row-column pair is nearly regular when every cross element is equal except possibly their intersection cell. A fully regular cross also qualifies. Return the number of qualifying row-column pairs. Function countNearlyRegularCrosses(matrix: int[][]) → int Examples Example 1 matrix = [[1,1],[1,2]] return = 2 The crosses centered at (0,0) and (1,1) have equal non-intersection elements. Example 2 matrix = [[7]] return = 1 A one-cell cross qualifies vacuously. Constraints 1 <= rows,columns <= 50
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to ignore the intersection cell completely. For a pair (r,c), collect row r without column c, plus column c without row r. Every value in that set must be identical. Brute force is rows times columns times (rows plus columns), about 250k operations at the limit, which is fine. The cleaner version keeps a frequency map for each row and each column. Subtract the intersection value, then check that each side has at most one distinct value and that the two sides agree if both are nonempty. Pitfalls: counting the intersection twice, and forgetting that an empty side is valid. A 1x1 matrix returns 1, and a 1xN matrix needs only the single row side. Don't reach for BFS or DFS, because there's no graph. If you freeze, StealthCoder can hand you the loop structure live.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Count Nearly Regular Matrix Crosses 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 by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass ZipRecruiter's OA.
ZipRecruiter reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count Nearly Regular Matrix Crosses FAQ
How hard is Count Nearly Regular Matrix Crosses really?+
Easy to medium. The grid is at most 50 by 50, so a brute-force check per cell passes comfortably. The difficulty is reading the rules correctly, mainly excluding the intersection and handling empty sides. Once you've got that, it's maybe fifteen lines of code.
What's the trick to solving it fast?+
Treat the cross as two lists with the intersection removed: the rest of the row and the rest of the column. All values in both lists must be equal. Take any one value as reference, compare everything against it, and count the pair if nothing differs.
Do I need BFS or DFS because it's a matrix?+
No. Nothing moves between cells and there's no connectivity question. Each row-column pair is judged independently. Frequency maps or a direct comparison loop is all it takes. Reaching for graph traversal here just adds code and bugs.
What edge cases should I test before submitting?+
Test a 1x1 matrix, which should return 1. Test a single row and a single column, where one side of the cross is empty. Test a matrix of all equal values, where every pair counts. Also check that the example [[1,1],[1,2]] returns 2, and try a case where the intersection differs from everything else.
How do I prepare for this in 48 hours?+
Write the brute-force version once from scratch and run it on the two examples. Then add the row and column frequency map version so you can explain the optimization. Practice counting problems on 2D arrays, since reading the rule precisely matters more than any algorithm here.