Workspace Rotate Image
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Rotate Image visualizer

Learn Rotate Image with a step-by-step Arrays visualizer, JavaScript source code and a worked example. Practice optimization and matrix.

INTERACTIVE EXECUTION STUDIO
LEARN BY DOING

Predict. Execute. Understand.

Follow the source code, inspect live state changes, and predict the next operation before revealing it.

1 / 7Execution step
Reference example · JavaScript
Inspect reference input
[
  [
    1,
    2,
    3
  ],
  [
    4,
    5,
    6
  ],
  [
    7,
    8,
    9
  ]
]

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = (m,s)=>{
for(let i=0;
i<m.length;
i++)for(let j=i+1;
j<m.length;
j++){
[m[i][j],m[j][i]]=[m[j][i],m[i][j]];
s("Transpose cells "+i+","+j,{
Matrix:m
})
}
for(let i=0;
i<m.length;
i++){
m[i].reverse();
s("Reverse row "+i,{
Matrix:m
})
}
return m
};

Execution canvas

Active Changed
STEP 1

Transpose cells 0,1

Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.

Matrix3 rows
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

Topic: Arrays · Optimization and matrix

Follow the computed execution on the reference input. Advance one step at a time, inspect the changing values and compare your prediction with the final output.

Example input

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

Execution steps

  1. Transpose cells 0,1
  2. Transpose cells 0,2
  3. Transpose cells 1,2
  4. Reverse row 0
  5. Reverse row 1
  6. Reverse row 2

Computed output

[
  [
    7,
    4,
    1
  ],
  [
    8,
    5,
    2
  ],
  [
    9,
    6,
    3
  ]
]
JavaScript reference implementation
const solve = (m,s)=>{
  for(let i=0;
  i<m.length;
  i++)for(let j=i+1;
  j<m.length;
  j++){
    [m[i][j],m[j][i]]=[m[j][i],m[i][j]];
    s("Transpose cells "+i+","+j,{
      Matrix:m
    })
  }
  for(let i=0;
  i<m.length;
  i++){
    m[i].reverse();
    s("Reverse row "+i,{
      Matrix:m
    })
  }
  return m
};

Continue learning

Practice related DSA questions in the same topic.