FastPrepPascal's Triangle

Pascal's Triangle

Ericsson logoEricsson● EasyFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Complete the function below. The function receives the number of rows and returns the generated Pascal's Triangle.

Problem Problem: Pascal's Triangle

Given a non-negative integer numRows, generate the first numRows rows of Pascal's Triangle and return them as a 2D array triangle.

Pascal's Triangle is defined as:

Row i (0-indexed) contains i+1 elements.

The first and last element of each row is 1.

For other positions: triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j] for 0 < j < i.

Input

A single integer: numRows

Output

Print a 2D array (you may print each row as a list) representing the first numRows rows.

Constraints 0 <= numRows <= 30

Example Input:

5

Output:

[

[1],

[1,1],

[1,2,1],

[1,3,3,1],

[1,4,6,4,1]

]

Example Input

0

Output

[]

Function

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

Complete solvePascalsTriangle. It has one parameter, int numRows. Return the first numRows rows of Pascal's Triangle as a 2D integer array.

Examples

Example 1

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

The returned string array must match the expected standard output lines for the sample input.

Constraints

  • Use the limits and requirements stated in the prompt.

More Ericsson problems

See Ericsson hiring insights
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…