Workspace Add Two Numbers
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Add Two Numbers visualizer

Learn Add Two Numbers with a step-by-step Linked Lists visualizer, JavaScript source code and a worked example. Practice rewiring and advanced.

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 / 4Execution step
Reference example · JavaScript
Inspect reference input
{
  "a": [
    2,
    4,
    3
  ],
  "b": [
    5,
    6,
    4
  ]
}

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = ({
a,b
},s)=>{
let c=0,o=[];
for(let i=0;
i<Math.max(a.length,b.length)||c;
i++){
let x=(a[i]||0)+(b[i]||0)+c;
o.push(x%10);
c=Math.floor(x/10);
s("Add corresponding digits",{
A:a,B:b,Output:o,Carry:[c]
})
}
return o
};

Execution canvas

Active Changed
STEP 1

Add corresponding digits

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

A3 items
B3 items
Output1 items
Carry1 items
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

Topic: Linked Lists · Rewiring and advanced

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

{
  "a": [
    2,
    4,
    3
  ],
  "b": [
    5,
    6,
    4
  ]
}

Execution steps

  1. Add corresponding digits

Computed output

[
  7,
  0,
  8
]
JavaScript reference implementation
const solve = ({
  a,b
},s)=>{
  let c=0,o=[];
  for(let i=0;
  i<Math.max(a.length,b.length)||c;
  i++){
    let x=(a[i]||0)+(b[i]||0)+c;
    o.push(x%10);
    c=Math.floor(x/10);
    s("Add corresponding digits",{
      A:a,B:b,Output:o,Carry:[c]
    })
  }
  return o
};

Continue learning

Practice related DSA questions in the same topic.