Spiral Matrix

Medium · Arrays & Strings · about 20 minutes

Asked at Apple, ByteDance, Bloomberg, Google, Amazon, Microsoft, Meta.

Given an m × n matrix, return all of its elements in spiral order: across the top row, down the right column, back along the bottom row, up the left column, then the same for the inner rectangle, and so on.

Related guides