Workspace Sudoku Solver
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Sudoku Solver visualizer

Learn Sudoku Solver with a step-by-step Recursion & Backtracking visualizer, JavaScript source code and a worked example. Practice constraint search.

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 / 10Execution step
Reference example · JavaScript
Inspect reference input
[
  "534678912",
  "672195348",
  "198342567",
  "859761423",
  "426853791",
  "713924856",
  "961537284",
  "287419635",
  "345286170"
]

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = (a,s)=>{
let b=a.map(r=>[...r]);
function f(){
for(let i=0;
i<9;
i++)for(let j=0;
j<9;
j++)if(b[i][j]==="0"){
for(let x of "123456789"){
let ok=!b[i].includes(x)&&b.every(r=>r[j]!==x);
for(let r=Math.floor(i/3)*3;
r<Math.floor(i/3)*3+3;
r++)for(let c=Math.floor(j/3)*3;
c<Math.floor(j/3)*3+3;
c++)if(b[r][c]===x)ok=false;
s("Test digit "+x,{
Grid:b,Cell:[i,j],Candidate:[x],Allowed:[ok]
});
if(ok){
b[i][j]=x;
if(f())return true;
b[i][j]="0"
}
}
return false
}
return true
}
f();
return b.map(r=>r.join(""))
};

Execution canvas

Active Changed
STEP 1

Test digit 1

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

Grid9 rows
Cell2 items
Candidate1 items
Allowed1 items
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

Topic: Recursion & Backtracking · Constraint search

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

[
  "534678912",
  "672195348",
  "198342567",
  "859761423",
  "426853791",
  "713924856",
  "961537284",
  "287419635",
  "345286170"
]

Execution steps

  1. Test digit 1
  2. Test digit 2
  3. Test digit 3
  4. Test digit 4
  5. Test digit 5
  6. Test digit 6
  7. Test digit 7
  8. Test digit 8
  9. Test digit 9

Computed output

[
  "534678912",
  "672195348",
  "198342567",
  "859761423",
  "426853791",
  "713924856",
  "961537284",
  "287419635",
  "345286179"
]
JavaScript reference implementation
const solve = (a,s)=>{
  let b=a.map(r=>[...r]);
  function f(){
    for(let i=0;
    i<9;
    i++)for(let j=0;
    j<9;
    j++)if(b[i][j]==="0"){
      for(let x of "123456789"){
        let ok=!b[i].includes(x)&&b.every(r=>r[j]!==x);
        for(let r=Math.floor(i/3)*3;
        r<Math.floor(i/3)*3+3;
        r++)for(let c=Math.floor(j/3)*3;
        c<Math.floor(j/3)*3+3;
        c++)if(b[r][c]===x)ok=false;
        s("Test digit "+x,{
          Grid:b,Cell:[i,j],Candidate:[x],Allowed:[ok]
        });
        if(ok){
          b[i][j]=x;
          if(f())return true;
          b[i][j]="0"
        }
      }
      return false
    }
    return true
  }
  f();
  return b.map(r=>r.join(""))
};

Continue learning

Practice related DSA questions in the same topic.