Skip to content
  • Dashboard
  • Review
  • Go: plan & chat
  • Roadmap
  • Patterns & lessons49
  • Sheets
  • Problems1.5k
  • Ascend
  • Toolkit
  • Core CS + SQL
  • System Design
  • Machine Coding
  • Interview hub
  • Mock interview
  • Companies & OA
  • Leaderboard
  • Battles
  • College Cup
  • People

Keep your progress

Sync across devices with a free account.

Sign upSign in
Patterns & lessons
Sign inSign up
Patterns/Arrays & Two Pointers/Two Pointers

Technique

Simulation & Matrix Traversal

Carry out exactly what the statement describes with careful index bookkeeping (layers, directions, in-place swaps).

0%
mastery
0/22 solved
Practise nextSpiral Matrix
IdeaPracticePitfalls

Recognise it

The signal

spiral/rotate/transpose a matrix

  1. 1Brute force

    The brute force is the solution; the skill is clean boundaries and in-place tricks.

  2. 2The observation

    Extra copies of the grid: encode old/new state in the same cell, or rotate by transpose + reverse.

  3. 3Why it works

    Problem-specific: the processed boundary (layer, direction) shrinks each step.

Reach for it when

  • +follow rules step by step with small limits
  • +game-of-life style updates in place

Not when

  • ×number of steps is huge (find a cycle with functional-graph, or a formula with math)

Practice

Next

Spiral Matrix

0/22 solved
  • Plus OneAdobe, GoogleEasy
  • Longest Common PrefixAdobe, AmazonEasy
  • Reverse StringApple, AdobeEasy
  • Roman to IntegerAmazon, AdobeEasy
  • Add BinaryMeta, AmazonEasy
  • Pascal's TriangleAmazon, Morgan StanleyEasy
  • Weird AlgorithmCSESEasy
  • Bit++Codeforces800
  • Following DirectionsCodeforces800
  • Run-Length Encode a StringGoldman Sachs, SalesforceOAEasy
  • Spiral MatrixMicrosoft, AmazonMedium
  • Rotate ImageAmazon, MicrosoftMedium

Pitfalls

Misconceptions

  • !off-by-one at layer boundaries in spiral order

Looks like it, isn't

  • Bitwise AND of Numbers Range needs Bit Tricks & Bitmasking
    Why
    Looping is O(range); the answer is the common binary prefix.
Go deeperresources

Resources

  • Two PointersUSACO Guide · course
  • Prefix SumsUSACO Guide · course
  • More on Prefix Sums (difference arrays)USACO Guide · course
  • Array cheatsheetTech Interview Handbook · article
  • NeetCode RoadmapNeetCode · sheet
  • Array and String explore cardLeetCode · course
TodayReviewPatternsProblems