15. Pascal's Triangle
easyAsked at SnowflakeGenerate the first numRows of Pascal's triangle. Snowflake uses this to test row-at-a-time DP building — the same shape as windowing operations over a column where the next row depends on the previous one.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Snowflake loops.
- Glassdoor (2025-Q4)— Snowflake intern phone screen used this as a DP warm-up.
- LeetCode Discuss (2025-08)— Reported at Snowflake new-grad screens.
Problem
Given an integer numRows, return the first numRows of Pascal's triangle. In Pascal's triangle, each number is the sum of the two numbers directly above it.
Constraints
1 <= numRows <= 30
Examples
Example 1
numRows = 5[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]Example 2
numRows = 1[[1]]Approaches
1. Compute via factorials
Each entry = C(n, k) computed with factorials.
- Time
- O(n^2)
- Space
- O(n^2)
function generate(numRows) {
function fact(n) { let r = 1n; for (let i = 2n; i <= BigInt(n); i++) r *= i; return r; }
const triangle = [];
for (let n = 0; n < numRows; n++) {
const row = [];
for (let k = 0; k <= n; k++) {
row.push(Number(fact(n) / (fact(k) * fact(n - k))));
}
triangle.push(row);
}
return triangle;
}Tradeoff: Recomputes factorials, expensive. Don't ship.
2. Build row from previous row (optimal)
Each row begins and ends with 1. Interior entries are sum of adjacent entries in the previous row.
- Time
- O(numRows^2)
- Space
- O(numRows^2)
function generate(numRows) {
const triangle = [];
for (let i = 0; i < numRows; i++) {
const row = new Array(i + 1).fill(1);
for (let j = 1; j < i; j++) {
row[j] = triangle[i - 1][j - 1] + triangle[i - 1][j];
}
triangle.push(row);
}
return triangle;
}Tradeoff: Linear in output size. Each row depends only on the previous — classic 'window of size 1' DP.
Snowflake-specific tips
Snowflake interviewers want the row-at-a-time DP framing, not the factorial formula. Bonus signal: connect to window functions in SQL — Pascal's triangle is the canonical 'each row's output depends on the previous row's output', which is how Snowflake implements LAG/LEAD windowing.
Common mistakes
- Computing each cell with factorials, which is O(n^2 * n) = O(n^3).
- Off-by-one in the row length (row i has i+1 elements, not i).
- Modifying the previous row in place — corrupts later reads.
Follow-up questions
An interviewer at Snowflake may pivot to one of these next:
- Pascal's Triangle II (LC 119) — return only the kth row in O(k) space.
- Count paths in a grid (related to C(n, k)).
- How does Snowflake compute LAG over a partition?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why is each row's first and last entry 1?
C(n, 0) and C(n, n) both equal 1 — there's exactly one way to choose 0 items or all items.
How does this relate to window functions?
Window functions like LAG(x) OVER (ORDER BY ts) produce a value for each row based on previous rows. The recurrence pattern — current depends on previous — is identical to Pascal's row construction.
Practice these live with InterviewChamp.AI
Drill Pascal's Triangle and other Snowflake interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →