Skip to the code
cf.
+25 XP

#77 Spiral Read of a Tile Grid

medium

A photo gallery's "story mode" reveals tiles in a spiral: along the top row left to right, down the right column, back along the bottom row, up the left column, and then the same for the inner tiles, until every tile is shown.

Write spiralRead(grid) that returns the tile values in that order.

Example 1

Input: grid = [[2,4,6,8],[10,12,14,16],[18,20,22,24]]
Output: [2,4,6,8,16,24,22,20,18,10,12,14]

The outer ring first, then the inner row 12, 14.

Example 2

Input: grid = [[1],[2],[3]]
Output: [1,2,3]

A single column is read straight down.

Constraints

  • 1 <= rows, cols <= 100
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).