Diagonal Traverse II

MediumMatrixsimulation

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given a jagged 2D list nums: row i may have a different length than other rows. The cell nums[i][j] lies on anti-diagonal number i + j. Return all values in a single list, ordered by anti-diagonal number ascending. Within the same anti-diagonal, list cells from the **largest row index to the smallest** (bottom-left to top-right). The total number of cells can be large, so avoid scanning positions that do not exist; aim for time linear in the number of cells.

Examples

Input: nums = [[1,2,3],[4,5,6],[7,8,9]]
Output: [1,4,2,7,5,3,8,6,9]
Diagonal 0 is 1; diagonal 1 is 4 then 2; diagonal 2 is 7, 5, 3; and so on.
Input: nums = [[1,2,3,4,5],[6,7],[8],[9,10,11],[12,13,14,15,16]]
Output: [1,6,2,8,7,3,9,4,12,10,5,13,11,14,15,16]
Missing cells are simply skipped; diagonal 4 reads 12 (row 4), 10 (row 3), 5 (row 0).

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i].length <= 10^5
  • Total number of cells <= 10^5
  • 1 <= nums[i][j] <= 10^5

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More simulation problems