Arrays · Coding problem

Pascal's Triangle

A free coding problem from the Pascal's Triangle Construction skill. Read it here, then solve it in the app in Java or Python.

  • Easy
  • Coding problem
  • Free
  • Java and Python

The problem

Print the first numRows rows of Pascal's Triangle. Every row starts and ends with 1, and each number in between is the sum of the two numbers diagonally above it. Row r (counting from 0) has r + 1 numbers.

Input format

A single integer numRows. Print each row on its own line, its values space-separated.

Example

Input:  numRows = 4
Output:
1
1 1
1 2 1
1 3 3 1

Constraints

  • 1 <= numRows <= 30

How it's graded

  • Run checks your code against the examples. Submit also runs a hidden suite in four groups - basic cases, edge cases, random inputs and performance tests at the size limit - and shows which groups pass, never the hidden inputs.
  • Stuck? Hints come in four steps, from a nudge to a code skeleton, each costing part of the points.
  • Once you pass, you see the approaches from brute force to optimal, and where your solution lands among them.

More problems in Pascal's Triangle Construction

Solve it Pascal's Triangle free

No card needed. Lessons, practice questions and easy problems are free forever.

Free plan, no card · 7-day Pro trial · Cancel anytime