Workspace Pascal Triangle
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Pascal Triangle visualizer

Learn Pascal Triangle 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 / 6Execution step
Reference example · JavaScript
Inspect reference input
5

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = (n,s)=>{
let o=[];
for(let i=0;
i<n;
i++){
let r=Array(i+1).fill(1);
for(let j=1;
j<i;
j++)r[j]=o[i-1][j-1]+o[i-1][j];
o.push(r);
s("Build row "+i,{
Triangle:o
})
}
return o
};

Execution canvas

Active Changed
STEP 1

Build row 0

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

Triangle1 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

5

Execution steps

  1. Build row 0
  2. Build row 1
  3. Build row 2
  4. Build row 3
  5. Build row 4

Computed output

[
  [
    1
  ],
  [
    1,
    1
  ],
  [
    1,
    2,
    1
  ],
  [
    1,
    3,
    3,
    1
  ],
  [
    1,
    4,
    6,
    4,
    1
  ]
]
JavaScript reference implementation
const solve = (n,s)=>{
  let o=[];
  for(let i=0;
  i<n;
  i++){
    let r=Array(i+1).fill(1);
    for(let j=1;
    j<i;
    j++)r[j]=o[i-1][j-1]+o[i-1][j];
    o.push(r);
    s("Build row "+i,{
      Triangle:o
    })
  }
  return o
};

Continue learning

Practice related DSA questions in the same topic.