Generate the first n rows of Pascal's triangle

Quick Overview

Return the first num_rows rows of Pascal's triangle, where each row starts and ends with 1 and every interior value is the sum of the two values above it. It tests building a table one row at a time from the previous row and careful index handling at the row edges.

Generate the first n rows of Pascal's triangle

Company: Fortinet

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an integer `num_rows`, return the first `num_rows` rows of Pascal's triangle as a list of integer lists. The triangle starts with the row `[1]`. Each row begins and ends with `1`, and each interior value is the sum of the two values immediately above it. All values in the result fit within a signed 32-bit integer. ### Function Signature ```python def generate(num_rows: int) -> list[list[int]]: ``` ### Rules - Row `i` (counting from 0) has `i + 1` values. For `0 < j < i`, its value at position `j` is `triangle[i - 1][j - 1] + triangle[i - 1][j]`. - Return the rows from top to bottom, with the values of each row in left-to-right order. ### Constraints - `1 <= num_rows <= 30` ### Examples ```text Input: num_rows = 5 Output: [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]] ``` Each interior value is the sum of the two values directly above it. In the last row, `4 = 1 + 3`, `6 = 3 + 3` and `4 = 3 + 1`. ```text Input: num_rows = 1 Output: [[1]] ``` Only the first row `[1]` is returned. ```text Input: num_rows = 2 Output: [[1], [1, 1]] ``` The second row has no interior positions, so it holds only the two edge values.

Overview: Return the first num_rows rows of Pascal's triangle, where each row starts and ends with 1 and every interior value is the sum of the two values above it. It tests building a table one row at a time from the previous row and careful index handling at the row edges.

Fortinet logo
Fortinet
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given an integer num_rows, return the first num_rows rows of Pascal's triangle as a list of integer lists.

The triangle starts with the row [1]. Each row begins and ends with 1, and each interior value is the sum of the two values immediately above it.

All values in the result fit within a signed 32-bit integer.

Function Signature

def generate(num_rows: int) -> list[list[int]]:

Rules

  • Row i (counting from 0) has i + 1 values. For 0 < j < i , its value at position j is triangle[i - 1][j - 1] + triangle[i - 1][j] .
  • Return the rows from top to bottom, with the values of each row in left-to-right order.

Constraints

  • 1 <= num_rows <= 30

Examples

Input: num_rows = 5
Output: [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]

Each interior value is the sum of the two values directly above it. In the last row, 4 = 1 + 3, 6 = 3 + 3 and 4 = 3 + 1.

Input: num_rows = 1
Output: [[1]]

Only the first row [1] is returned.

Input: num_rows = 2
Output: [[1], [1, 1]]

The second row has no interior positions, so it holds only the two edge values.