Generating Pascal's Triangle

Given an integer N, generate a 2-D array list representing Pascal's Triangle up to the N-th row.

An element in Pascal's Triangle is the sum of the two elements directly above it in the previous row. The triangle also represents binomial coefficients.

Input:

The first line contains an integer T, the number of test cases.
Each test case consists of a single integer N, representing the number of rows in the Pascal's Triangle to be returned.

Output:

For each test case, return a 2-D array list containing Pascal's Triangle up to row N.

Example:

Input:
T = 1 N = 4
Output:
[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1]]
Explanation:

For N = 4, the Pascal's Triangle is:
[
[1],
[1, 1],
[1, 2, 1],
[1, 3, 3, 1]
]
Each element is obtained by summing the two directly above it.

Constraints:

  • 1 <= T <= 40
  • 1 <= N <= 50
  • Time Limit: 1 sec
Note:
The implementation function should return the result, not print it.
AnswerBot
4mo

Generate Pascal's Triangle up to N-th row using 2-D array list.

  • Create a 2-D array list to store the Pascal's Triangle elements.

  • Iterate through each row and calculate the elements based on the sum of t...read more

Help your peers!
Select
Add answer anonymously...
INCA INFOTECH TECHNOLOGIES Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits