FastPrepSpiral Matrix Traversal
Problems/Spiral Matrix Traversal

Spiral Matrix Traversal

Microsoft logoMicrosoft● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Microsoft onsite interview. The report does not say where the counterclockwise walk starts. Our version is about 97% the same as the reported question.

Given a non-empty rectangular integer matrix matrix and a boolean clockwise, return all values in spiral order.

  • When clockwise is true, start at the top-left cell and initially move right.
  • When clockwise is false, start at the top-left cell and initially move down.
  • Visit every cell exactly once.

Function

spiralOrder(matrix: int[][], clockwise: boolean) → int[]

Examples

Example 1

matrix = [[1,2,3],[4,5,6],[7,8,9]]clockwise = truereturn = [1,2,3,6,9,8,7,4,5]
matrix
012012314562789

The traversal follows the top row, right edge, bottom row, left edge, and then the center.

Example 2

matrix = [[1,2,3,4,5]]clockwise = falsereturn = [1,2,3,4,5]
matrix
01234012345

A single row has only one possible visit order from the top-left cell.

Constraints

  • matrix has at least one row and one column.
  • Every row has the same number of columns.
  • Every matrix value is a signed integer.

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

  • Random Bingo Card GeneratorUber · ONSITE INTERVIEWUber interview questions
  • Rectangle Point Coverage CountsOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
  • Spiral MatrixAthenahealth · ONSITE INTERVIEW
  • Explore an Unknown Grid and Find a PathLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
  • Spiral MatrixNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
  • Tic-Tac-Toe Move WinnersByteDance · ONSITE INTERVIEWByteDance interview questions
  • Trace Every Water Drop to Its Resting DestinationGoogle · ONSITE INTERVIEWGoogle interview questions
  • Stochastic Gradient Descent with MomentumMeta · ONSITE INTERVIEWMeta interview questions
  • Cyclic Fuel Command SimulationWaymo · ONSITE INTERVIEWWaymo interview questions
  • Spiral Matrix with ObstaclesTemu · ONSITE INTERVIEWTemu interview questions

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[] spiralOrder(int[][] matrix, boolean clockwise) {
        // TODO: return every matrix value in the requested spiral order.
    }
}
Checking account…