Longest Border-Ending Diagonal Pattern
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The constraint tells you what ZipRecruiter wants here: O(n^2 * m^2) is allowed, so nobody expects a clever trick, just a clean walk along diagonals. This one was reported in September 2024, and it's a matrix problem dressed up as a pattern-matching puzzle. You find every cell holding a 1, shoot a ray in each of four diagonal directions, and check the sequence 1, 2, 0, 2, 0 as you go. The catch is the border-ending rule. If you can't solve it cold, StealthCoder runs invisibly during the live OA as a safety net while you work out the logic yourself.
The problem
For this exercise, use the callable contract below. Given a rectangular integer matrix matrix, find the longest diagonal segment that matches the infinite pattern 1, 2, 0, 2, 0,.... A valid segment: starts at any matrix cell whose value is 1; continues in exactly one of the four diagonal directions; matches 2 after the initial 1, then alternates 0 and 2; and ends at a cell on the first row, last row, first column, or last column. Return the maximum length of a valid segment. Function longestBorderDiagonal(matrix: int[][]) → int Examples Example 1 matrix = [[0,0,1,2],[0,2,2,2],[2,1,0,1]] return = 3 Starting at matrix[2][3] and moving up-left produces 1, 2, 0 at cells (2,3), (1,2), and (0,1). The last cell is on the first row, so this is a valid length-3 segment. No longer valid segment exists. Constraints matrix is non-empty and rectangular. Every matrix value is 0, 1, or 2. A segment moves by one row and one column at each step and never changes direction. A solution with time complexity no worse than O(matrix.length^2 * matrix[0].length^2) fits the source's execution limit.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that there's no real DP needed. The limit is O(rows^2 * cols^2), so brute force is fine. For each cell equal to 1, try all four diagonal directions. Step one cell at a time and track the expected value. After the 1, expect 2, then 0, then 2, then 0, and so on. Stop the moment a cell breaks the pattern or you leave the grid. Only count the segment if the last matched cell sits on the first row, last row, first column, or last column. The common pitfall is counting a segment that matches the pattern but stops in the interior. Another is a single 1 on a border cell. That's a valid length-1 segment, since it starts and ends on the border. Keep the max across all starts. If you blank on the expected-value indexing, StealthCoder is the hedge during the live OA.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Longest Border-Ending Diagonal Pattern 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 by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as length of longest v shaped diagonal segment. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass ZipRecruiter's OA.
ZipRecruiter reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Longest Border-Ending Diagonal Pattern FAQ
What's the trick in the Longest Border-Ending Diagonal Pattern problem?+
Brute force works. From each cell with value 1, walk all four diagonals and check the 1, 2, 0, 2, 0 pattern. The only real catch is that a segment only counts if its last matching cell lies on a matrix border. Track the max of valid ones.
Do I need dynamic programming for this?+
No. The stated bound of O(rows^2 * cols^2) means plain simulation passes. DP with memoized direction states could speed it up, but it adds bug risk. In a timed OA, the direct ray walk is the safer choice.
How do I check the pattern 1, 2, 0, 2, 0 cleanly?+
The first cell must be 1. After that, use the step index. Odd steps expect 2. Even steps expect 0. So for step k at or above 1, expected is 2 if k is odd and 0 if k is even. Break at the first mismatch.
What edge cases trip people up?+
A lone 1 on a border cell is a valid length-1 segment. A long pattern match that ends mid-grid is invalid. You also have to check the border at each step, since a segment can stop at a border cell only if the pattern held up to it.
How should I prepare in 48 hours for this ZipRecruiter OA?+
ZipRecruiter reported this in September 2024. Write the four-direction loop with a direction array, then test on the sample. Practice matrix bounds checks and the border condition. Then do a couple of other grid-walk problems so the indexing feels automatic.