Pascal's Triangle Construction
You are provided with an integer 'N'. Your task is to generate a 2-D list representing Pascal’s triangle up to the 'N'th row.
Pascal's triangle is a triangular array where each element is the sum of the two directly above it from the preceding row, representing binomial coefficients.
Example:
Input:
N = 4
Output:
[ [1], [1, 1], [1, 2, 1], [1, 3, 3, 1] ]
Explanation:
In the third row, for example, the second element is calculated as follows: 2 = 1 + 1. Similarly, in the fourth row, the numbers 3 are derived: 3 = 1 + 2 and 3 = 2 + 1.
Constraints:
1 ≤ T ≤ 40
1 ≤ N ≤ 50
- Time Limit: 1 sec
Saurabh Kumar
1y
currently not working
answer = [] for row in range(numRows): ans = 1 ansRow = [1] for col in range(row): ans *= row - col ans //= col + 1 ansRow.append(ans) answer.append(ansRow) return answer
Help your peers!
Add answer anonymously...
Top Capgemini Senior Software Engineer interview questions & answers
Popular interview questions of Senior Software Engineer
Top HR questions asked in Capgemini Senior Software Engineer
>
Capgemini Senior Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app