Find Longest Diagonal Segment
Reported by candidates from Roblox's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Roblox's diagonal segment problem showed up in August 2024 and it's testing whether you can track state across a grid without overthinking it. You're looking for the longest sequence of cells you can visit while moving diagonally. The trap is treating this like a path problem when it's really a sequence problem. StealthCoder will have this pattern locked in if you blank on the state transitions, so you can focus on nailing the implementation.
Pattern and pitfall
This is a dynamic programming grid problem where you build up the longest diagonal sequence ending at each position. The key insight is that a diagonal move from (r, c) to (r+1, c+1) or (r+1, c-1) only extends the previous diagonal if the values satisfy your constraint, otherwise you restart at 1. Most candidates waste time on recursive backtracking or try to precompute all diagonals. Instead, iterate through the grid bottom-up or use memoization keyed on (row, col, direction). StealthCoder catches the state definition fast: dp[r][c] = longest diagonal ending here, not longest possible anywhere. That one mistake kills half the attempts.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Find Longest Diagonal Segment 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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as longest increasing path in a matrix. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Roblox's OA.
Roblox reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Find Longest Diagonal Segment FAQ
What counts as a valid diagonal move in Roblox's version?+
You typically move from (r, c) to (r+1, c+1) or (r+1, c-1), staying within bounds. The problem usually specifies a numeric constraint on adjacent cells, like increasing values or matching a condition. Re-read the exact rule before coding.
Should I use recursion with memoization or bottom-up DP?+
Either works. Bottom-up is safer for time since you avoid call stack overhead. Memoize on (row, col, direction) to track which diagonal you're on. Top-down is easier to debug if you blank, so pick your comfort zone.
How do I handle the grid boundaries without off-by-one errors?+
Check r+1 < rows and c+1 < cols (or c-1 >= 0) before accessing the next cell. Set up your loop to start from row 0 or rows-1 depending on which direction you iterate. Sketch a 3x3 example on paper first.
What's the common mistake that tanks submissions?+
Confusing the longest diagonal from a given starting position with the longest diagonal segment overall. You need to track the best answer across all positions and directions, not just return the first result you find.
Can I solve this in a single pass, or do I need multiple iterations?+
A single pass works if you iterate in the right order (usually bottom-up for diagonals). If you go top-down, you risk accessing uncomputed cells. Plan your iteration order before you code to avoid re-scanning.