FastPrepDiagonal Matrix Iterator
Problems/Diagonal Matrix Iterator

Diagonal Matrix Iterator

Microsoft logoMicrosoft● MediumNEW GRADONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamples

Problem statement

Source note: Reported in a Microsoft new grad onsite interview. The report asks for walking a matrix along its diagonals as an iterator class. It leaves out the direction of the walk, how to handle a non-square matrix, and how to run it. Our version is about 78% the same as the reported question.

Given a non-empty rectangular integer matrix matrix, return the sequence produced by a diagonal iterator over all of its elements.

Number diagonals by row + column, starting with diagonal 0 at the top-left cell. The iterator traverses even-numbered diagonals upward and to the right, and odd-numbered diagonals downward and to the left.

The iterator begins at the top-left cell, yields every matrix element exactly once, and is exhausted immediately after yielding the bottom-right cell.

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]
matrix
012012314562789

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]
matrix
01201231456

The traversal alternates direction across the four diagonals of the rectangular matrix.

More Microsoft Onsite Interview questions

  • Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
  • Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
  • Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
  • Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
  • Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
  • Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
  • Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
  • Coin ChangeONSITE INTERVIEW · Seen Oct 2026
  • Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
  • Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026

Similar questions at other companies

  • Spiral MatrixAthenahealth · ONSITE INTERVIEW
  • Spiral MatrixNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
  • Incremental Hard Attention with a KV CacheOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
  • Stochastic Gradient Descent with MomentumMeta · ONSITE INTERVIEWMeta interview questions
  • Minesweeper Board UpdateUber · ONSITE INTERVIEWUber interview questions
  • Game of Life: In-Place Next GenerationGoogle · ONSITE INTERVIEWGoogle interview questions
  • Candy Crush Board Until StableBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
  • Game of Life Next GenerationTemporal · ONSITE INTERVIEWTemporal interview questions
  • Minesweeper Board UpdateClay · ONSITE INTERVIEW
  • Minesweeper Board UpdateAirbyte · ONSITE INTERVIEW

Microsoft interview experiences

  • Microsoft OA Questions, October 2026: 4 Experiences
  • Microsoft Experienced SWE Phone Screen: 1 Experience
  • Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
  • Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
  • Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
  • Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)
All Microsoft interview questionsSee Microsoft hiring insights
Saved
class Solution {
    public int[] findDiagonalOrder(int[][] matrix) {
        // write your code here
    }
}
Checking account…