15. Pascal's Triangle
easyAsked at SalesforceGiven numRows, return the first numRows of Pascal's triangle. Salesforce asks this to test row-by-row dynamic-programming style construction.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Salesforce loops.
- Glassdoor (2026-Q1)— Asked as an easy warmup before harder DP problems.
- LeetCode Discuss (2025)— Salesforce uses this pattern internally for cumulative-row reporting.
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 factorial / combinations
Each cell is C(n, k); compute directly 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 = (n, k) => fact(n) / (fact(k) * fact(n - k));
const rows = [];
for (let i = 0; i < numRows; i++) {
rows.push(Array.from({ length: i + 1 }, (_, j) => C(i, j)));
}
return rows;
}Tradeoff: Factorials overflow quickly and recompute work. Salesforce will push you to the iterative additive version.
2. Row-by-row additive construction
Each row starts and ends with 1; interior cells are the sum of the two above. Build iteratively from previous row.
- Time
- O(n^2)
- Space
- O(n^2) for output
function generate(numRows) {
const rows = [];
for (let i = 0; i < numRows; i++) {
const row = new Array(i + 1).fill(1);
for (let j = 1; j < i; j++) {
row[j] = rows[i - 1][j - 1] + rows[i - 1][j];
}
rows.push(row);
}
return rows;
}Tradeoff: No factorial, no overflow, and each cell is computed in O(1) from prior row. Standard DP construction.
Salesforce-specific tips
Salesforce values the additive over the factorial approach because their financial calculations frequently use prior-row data to derive next-row data (cumulative compensation, vested options, etc.). Bonus signal: mention you could compress to O(n) space if only the last row is needed.
Common mistakes
- Allocating row[i+1] of zeros instead of ones — interior assignment then overwrites correctly but edges are 0.
- Looping j from 0 instead of 1 — overwrites the leading 1 with 0+something.
- Using BigInt unnecessarily — Pascal's row 30 fits in Number.
Follow-up questions
An interviewer at Salesforce 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 (DP using Pascal's coefficients).
- Generate the nth row using the C(n, k) recurrence in O(n) time.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why fill with 1s upfront?
Both edges of every row are 1, so initializing the whole row to 1 saves a special case for edges. The interior loop overwrites the right cells.
Could I use only O(n) extra memory?
Yes for LC 119 (return only row k), by updating in place from right to left. For LC 118 you need to store all rows for the output.
Practice these live with InterviewChamp.AI
Drill Pascal's Triangle and other Salesforce interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →