FastPrepPascal's Triangle
Problem · Dynamic Programming

Pascal's Triangle

Learn this problem
EasyAmazon logoAmazonNEW GRADONSITE INTERVIEW
See Amazon hiring insights

Problem statement

Given a nonnegative integer numRows, return the first numRows rows of Pascal's Triangle.

Rows are zero-indexed. Row i contains i + 1 values; its first and last values are 1, and every interior value equals the sum of the two values above it.

Function

solvePascalsTriangle(numRows: int) → int[][]

Examples

Example 1

numRows = 5return = [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]

Each row begins and ends with 1; every interior value is the sum of its two parents from the preceding row.

Example 2

numRows = 0return = []

Zero requested rows produce an empty triangle.

Example 3

numRows = 1return = [[1]]

The first row contains only the boundary value 1.

Constraints

  • 0 <= numRows <= 30.
  • Return an empty matrix when numRows = 0.

More Amazon problems

drafts saved locally
public int[][] solvePascalsTriangle(int numRows) {
    // write your code here
}
numRows5
expected[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]
checking account