Back to questions

Spiral Matrix Walmart Python Interview Question

Spiral Matrix

Walmart Python Interview Question

Given an m x n matrix, return all elements of the matrix in spiral order.

In case you don’t think about spirals that often (unless you're pondering galaxies or those satisfying snail shells), here’s what we mean: start at the top-left corner and move right across the first row, then down the last column, left along the bottom row, and finally back up the first column. Keep spiraling inward until you’ve collected all the elements.

It’s like peeling layers off an onion… but way less tear-inducing!

Example #1

Input: matrix = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]

Output: [1, 2, 3, 6, 9, 8, 7, 4, 5]

![Spiral Matrix Example 1 DataLemur]

Example #2

Input: matrix = [[1, 2, 3],[8, 9, 4],[7, 6, 5]]

Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]

Spiral Matrix Example 2 DataLemur

Input

Python

Output