DSA Factory
Free lessonsProgramming basics · Stage 9 · Mastery · Step 3

A row of Pascal's triangle

Build one row of a number triangle from the row above it. About 12 minutes.

No label this time

Pascal's triangle starts with a single 1 at the top. Every row after that begins and ends with 1, and each number in the middle is the sum of the two numbers just above it. Row 0 is 1. Row 1 is 1, 1. Row 2 is 1, 2, 1. Row 3 is 1, 3, 3, 1.

A small hint for this one. You don't need the whole triangle. To make a new row, you only ever need the row just before it, so you can keep just one row and replace it each time.

Before coding: what do I need to keep, and what can I throw away?
Building Pascal's triangle
0
1
1
2
3
4
row
0

Row 0 is just a single 1.

Move 1 of 5
Quick check

Row 3 of Pascal's triangle is 1, 3, 3, 1. What is the second number of row 4?

  1. A4
  2. B3
  3. C6
Show the answer

4. It is the sum of the two numbers above it: 1 + 3 = 4.

Your problem

Pascal's row

Return row n of Pascal's triangle as a list. Row 0 is [1]. Every row starts and ends with 1, and each other number is the sum of the two numbers above it in the previous row.

Example
n = 4 → [1, 4, 6, 4, 1]

0 ≤ n ≤ 30

Solve it in your browserPython, C++, Java or JavaScript. Hints if you get stuck. No sign-up needed.
Start coding