Skip to the editor
JSGroundwork
JSGroundwork handwritten · web dev
←Back to Matrix problems

Ready to read

JSJavaScript⑂Git◎Interview prepΣDSA in JSSDSystem Design
More topics15›
</>HTML{ }CSS⚛ReactNNext.jsNeNest.jsTSTypeScriptNoNode.js🐳DockerDBSQL & Databases✓Testing🔒Web Security☁Cloud & DevOps◈GraphQL◆Redis☸Kubernetes
☰‹›
Description
All topics›JavaScript›Matrix problems

Spiral Matrix

beginnerlayer I12 · Matrix problems5 tests

Given an m × n matrix, return all of its values in spiral order: left to right across the top, down the right side, right to left along the bottom, up the left side, then inwards and repeat.

  • The matrix need not be square
  • A single row or a single column is a valid input
  • An empty matrix returns an empty array
  • Every value appears in the output exactly once

Stuck?

All exercisesPlayground
1234567891011121314
loading the editor…

Nothing yet — hit Run and whatever you log shows up here.

Submit to see how you did.

⌘/Ctrl + Enter run⌘/Ctrl + S save⌘/Ctrl + / comment⌘/Ctrl + F find/replace⌘/Ctrl + D select next matchAlt + click multi-cursorTab indent · ⇧Tab outdentEsc leave fullscreen