Reported November 2020
SambaNova Systemsmatrix

Diagonal Traverse

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

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

Every cell gets returned exactly once, so the floor is O(m*n) and anything that rescans the matrix for each diagonal is wasted work. That's the Diagonal Traverse problem SambaNova Systems candidates reported in November 2020. It's a matrix simulation question, not a clever-algorithm question. The trap is the direction flips and the corner bounces on rectangular grids, not the idea itself. If you've seen the classic version, this is the same traversal. If you haven't, the trick fits in one sentence below. And if you blank mid-assessment, StealthCoder runs invisibly as a safety net and hands you the solution in real time.

The problem

Given a non-empty rectangular integer matrix matrix, return all of its elements in diagonal order.
Number diagonals by row + column, starting with diagonal 0 at the top-left cell. Traverse even-numbered diagonals upward and to the right. Traverse odd-numbered diagonals downward and to the left. Continue until every matrix element has been returned exactly once.

Function
findDiagonalOrder(matrix: int[][]) → int[]

Examples
Example 1
matrix = [[1,2,3],[4,5,6],[7,8,9]]
return = [1,2,4,7,5,3,6,8,9]
The diagonals are visited as [1], [2,4], [7,5,3], [6,8], and [9].
Example 2
matrix = [[1,2,3],[4,5,6]]
return = [1,2,4,5,3,6]
The traversal alternates direction across the four diagonals of the rectangular matrix.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: cells on the same diagonal share row + column. Loop d from 0 to m + n - 2. For each d, the rows run from max(0, d - n + 1) to min(d, m - 1), and the column is d - row. Walking rows in ascending order gives you downward and to the left, which is what odd diagonals need. For even diagonals, reverse that list or walk rows descending. That's O(m*n) time with no special casing at the edges. The common pitfall is the single-pointer simulation where you flip direction at the borders. It breaks on corners and on non-square matrices like the 2x3 example, where the bounce rules get fiddly. Test the 2x3 case by hand before submitting. If the bounds math slips under time pressure, StealthCoder is the hedge during the live OA, since it reads the prompt and gives you working code.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Diagonal Traverse 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 StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as diagonal traverse. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass SambaNova Systems's OA.

SambaNova Systems 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.

Diagonal Traverse FAQ

What's the trick to Diagonal Traverse?+

Group cells by row + column. Each sum is one diagonal. Loop the sums from 0 to m + n - 2, collect the cells on each, and reverse the order on even diagonals since those go upward and to the right. No simulation of bouncing needed.

How hard is this one really?+

Easy to medium. The idea is simple, but the index bounds are where people lose time. Getting the start and end row for each diagonal right on a rectangular matrix is the part that trips candidates up, not the algorithm.

What complexity should I aim for?+

O(m*n) time, since every element must be output once. Extra space beyond the result array can be O(1) if you walk rows in the right direction instead of building and reversing temporary lists. Both versions are fine for most graders.

Which edge cases should I test?+

A single row, a single column, a 1x1 matrix, and a non-square case like the 2x3 example. Those catch bad bounds and wrong direction on the last diagonals. The 3x3 example alone won't expose rectangular bugs.

How do I prepare for this in 48 hours?+

Write it once from scratch using the row + column grouping, then run both given examples by hand. Check the direction rule: odd goes downward-left, even goes upward-right. Then do one more matrix traversal problem so the index math stays fresh.

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

OA at SambaNova Systems?
Invisible during screen share
Get it