Workspace Word Ladder
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Word Ladder visualizer

Learn Word Ladder with a step-by-step Graphs visualizer, JavaScript source code and a worked example. Practice traversal and connectivity.

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 / 6Execution step
Reference example · JavaScript
Inspect reference input
{
  "start": "hit",
  "end": "cog",
  "words": [
    "hot",
    "dot",
    "dog",
    "lot",
    "log",
    "cog"
  ],
  "all": false
}

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = ({
start,end,words,all
},s)=>{
let remaining=new Set(words),q=[[start]],o=[];
while(q.length){
let next=[],used=new Set;
for(let p of q){
let u=p.at(-1);
if(u===end)o.push(p);
else for(let v of remaining)if([...u].filter((x,i)=>x!==v[i]).length===1){
next.push(p.concat(v));
used.add(v)
}
}
s("Expand shortest transformation paths",{
Paths:q,Next:next,Solutions:o
});
if(o.length)return all?o:o[0].length;
for(let v of used)remaining.delete(v);
q=next
}
return all?[]:0
};

Execution canvas

Active Changed
STEP 1

Expand shortest transformation paths

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

Paths1 rows
Next1 rows
Solutions0 items

Empty at this step.

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

Worked example and algorithm walkthrough

Topic: Graphs · Traversal and connectivity

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

{
  "start": "hit",
  "end": "cog",
  "words": [
    "hot",
    "dot",
    "dog",
    "lot",
    "log",
    "cog"
  ],
  "all": false
}

Execution steps

  1. Expand shortest transformation paths

Computed output

5
JavaScript reference implementation
const solve = ({
  start,end,words,all
},s)=>{
  let remaining=new Set(words),q=[[start]],o=[];
  while(q.length){
    let next=[],used=new Set;
    for(let p of q){
      let u=p.at(-1);
      if(u===end)o.push(p);
      else for(let v of remaining)if([...u].filter((x,i)=>x!==v[i]).length===1){
        next.push(p.concat(v));
        used.add(v)
      }
    }
    s("Expand shortest transformation paths",{
      Paths:q,Next:next,Solutions:o
    });
    if(o.length)return all?o:o[0].length;
    for(let v of used)remaining.delete(v);
    q=next
  }
  return all?[]:0
};

Continue learning

Practice related DSA questions in the same topic.