Reported October 2025
Duolingomatrix

Longest Border-Ending Diagonal Pattern

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

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

The constraint on this Duolingo OA, reported October 2025, quietly tells you brute force from every cell and every direction is fine if you stop at the border, but you still want something cleaner. You get a matrix of 0s, 1s and 2s and need the longest diagonal that starts at a 1, follows 2, 0, 2, 0 and ends on an edge cell. It's a diagonal walk with a pattern check, and it has a light DP flavor. If you blank on the live OA, StealthCoder runs invisibly as a safety net, but the logic below is short enough to own.

The problem

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

Here's the trick. Only cells with value 1 can start a segment. From each one, try all four diagonal directions. Step one cell at a time and check the expected value: position 1 must be 2, position 2 must be 0, position 3 must be 2, and so on. Expected value for step k (k>=1) is 2 if k is odd, 0 if k is even. Stop the moment a cell is out of bounds or mismatched. The segment only counts if the last matched cell sits on the first row, last row, first column, or last column. That's the pitfall: a long matching run that ends in the interior is invalid, so don't take the max blindly. Also note a lone 1 on the border has length 1 and counts as valid. Worst case cost is cells times 4 times the diagonal length, well inside the stated bound. If you blank during the live OA, StealthCoder is the hedge, but the walk is only a few lines.

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 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. 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 Duolingo's OA.

Duolingo 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.

Longest Border-Ending Diagonal Pattern FAQ

What's the trick in the Duolingo longest border-ending diagonal problem?+

Start only from cells equal to 1, walk each of the four diagonals, and compare each step to the pattern 2, 0, 2, 0. Only record the length when the walk ends on a border cell. Interior endings don't count, even if the pattern matched.

Do I need real dynamic programming here?+

Not strictly. The stated complexity allowance means a direct simulation from every 1 in every direction passes. DP or memoization on direction and position can speed it up, but it's optional. Simple, correct simulation beats a clever buggy table.

What edge cases break most solutions?+

A single 1 on the border is a valid segment of length 1. A matching run that stops in the interior must be discarded. Also watch the pattern index: after the initial 1, odd steps expect 2 and even steps expect 0. Off-by-one errors here are the usual killer.

How hard is this really?+

Easy to medium. The logic is a bounded walk with a parity check. The difficulty is reading the rules carefully, especially the border-ending requirement, not the algorithm. Most candidates who lose points miss that one condition.

How do I prepare in 48 hours for a problem like this?+

Practice matrix direction arrays like (1,1), (1,-1), (-1,1), (-1,-1) and bounds checks. Write the walk loop once from scratch, then test it on the sample where the answer is 3. Then test a lone border 1 and an interior dead end.

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

OA at Duolingo?
Invisible during screen share
Get it