12. Pascal's Triangle
easyAsked at SoFiGenerate the first numRows of Pascal's triangle — SoFi uses this to test whether candidates can derive each row from the previous, a pattern that mirrors compounding interest schedules.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given an integer numRows, return the first numRows of Pascal's triangle, where each number is the sum of the two numbers directly above it.
Constraints
1 <= numRows <= 30
Examples
Example 1
5[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]Example 2
1[[1]]Approaches
1. Brute force combinatorial
Compute each entry as C(r, c) using factorials.
- Time
- O(n^2)
- Space
- O(n^2)
function generate(numRows) {
const fact = n => n <= 1 ? 1 : n * fact(n-1);
const c = (r,k) => fact(r)/(fact(k)*fact(r-k));
return Array.from({length: numRows}, (_, r) =>
Array.from({length: r+1}, (_, k) => c(r,k))
);
}Tradeoff:
2. Iterative row-from-previous
Each row starts and ends with 1; middle entries are sums of adjacent pairs from the previous row.
- Time
- O(n^2)
- Space
- O(n^2)
function generate(numRows) {
const res = [];
for (let i = 0; i < numRows; i++) {
const row = new Array(i + 1).fill(1);
for (let j = 1; j < i; j++) {
row[j] = res[i-1][j-1] + res[i-1][j];
}
res.push(row);
}
return res;
}Tradeoff:
SoFi-specific tips
SoFi appreciates iterative-from-previous solutions — it's exactly how interest accrual works (each day's balance is yesterday's balance plus daily interest), so candidates who think in recurrences score higher.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Practice these live with InterviewChamp.AI
Drill Pascal's Triangle and other SoFi interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →