15. Pascal's Triangle
easyAsked at VercelGenerate the first numRows of Pascal's triangle. Vercel asks this for the row-from-previous-row recurrence — same pattern they use to compute layer-by-layer rendering costs in their build dependency tree.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Vercel loops.
- Glassdoor (2025-11)— Vercel new-grad warm-up question.
- LeetCode Discuss (2026-Q1)— Mentioned in Vercel interview prep notes.
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 binomial coefficients directly
Use the formula C(n, k) = n! / (k! * (n-k)!) for each cell.
- Time
- O(n^2)
- Space
- O(n^2)
function generate(numRows) {
const fact = (n) => n <= 1 ? 1 : n * fact(n - 1);
const out = [];
for (let n = 0; n < numRows; n++) {
const row = [];
for (let k = 0; k <= n; k++) {
row.push(fact(n) / (fact(k) * fact(n - k)));
}
out.push(row);
}
return out;
}Tradeoff: Correct but uses factorials, which overflow on larger numRows in non-BigInt languages. Mention the recurrence as the cleaner option.
2. Row-from-previous-row recurrence (optimal)
Each row starts and ends with 1. Inside, row[i] = prev[i-1] + prev[i].
- Time
- O(n^2)
- Space
- O(n^2) output
function generate(numRows) {
const out = [];
for (let n = 0; n < numRows; n++) {
const row = new Array(n + 1).fill(1);
for (let k = 1; k < n; k++) {
row[k] = out[n - 1][k - 1] + out[n - 1][k];
}
out.push(row);
}
return out;
}Tradeoff: Cleaner and avoids the factorial overflow. The two-end-1 invariant means k only iterates the interior.
Vercel-specific tips
Vercel grades the recurrence version. Bonus signal: noting that you only need the previous row in memory (LC 119, single-row variant), and connecting this to dynamic-programming roll-back patterns. Also flag that you'd preallocate the row to avoid push allocations in a hot path.
Common mistakes
- Iterating k from 0 to n inclusive — needs to skip the endpoints since they're already 1.
- Using push() inside the inner loop — causes resizing; preallocate with new Array(n+1).fill(1).
- Forgetting to handle numRows === 0 — the constraint says >= 1 so it's fine, but flag the assumption.
Follow-up questions
An interviewer at Vercel may pivot to one of these next:
- Return only the k-th row (LC 119) — O(k) space.
- Find the k-th entry of the n-th row in O(k) time and O(1) space.
- How does Pascal's triangle relate to combinatorics? (n choose k)
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why preallocate with fill(1)?
The two endpoints are already 1, and preallocating avoids the push() overhead. In a hot loop this matters; in an interview it shows you think about JS engine internals.
Can I do this in O(n) space?
Yes — for LC 119 (single row) you only need the previous row. Iterate from right to left in place to compute the next row in O(k) extra space.
Practice these live with InterviewChamp.AI
Drill Pascal's Triangle and other Vercel interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →