16. Pascal's Triangle
easyAsked at WorkdayGenerate the first numRows of Pascal's triangle. Workday uses this to test 2D-array construction and row-from-previous-row dependency — analogous to month-over-month payroll deltas.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Workday loops.
- Glassdoor (2025)— Workday SDE1 phone screen.
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. Binomial coefficient formula
Compute C(n, k) directly for each cell.
- Time
- O(numRows^2)
- Space
- O(numRows^2) output)
// row[i][j] = factorial(i) / (factorial(j) * factorial(i-j))
// integer overflow for n=30 with naive factorialTradeoff: Correct but factorials overflow quickly without BigInt and you compute the same things repeatedly.
2. Iterative row build
Start each row with [1]. For inner positions, sum the two values above. End with [1].
- Time
- O(numRows^2)
- Space
- O(numRows^2) output)
function generate(numRows) {
const out = [];
for (let i = 0; i < numRows; i++) {
const row = new Array(i + 1).fill(1);
for (let j = 1; j < i; j++) {
row[j] = out[i - 1][j - 1] + out[i - 1][j];
}
out.push(row);
}
return out;
}Tradeoff: No factorial overhead. Each cell read from the previous row directly.
Workday-specific tips
Workday grades for index discipline — the inner loop runs from j=1 to j<i, NOT j<=i. Pre-filling with 1 lets you skip writing the boundary cells. Articulate the invariant.
Common mistakes
- Off-by-one in the inner loop (j <= i causes overwrite of the trailing 1).
- Forgetting that row[0] and row[i] are both 1 — over-engineered initialization.
- Reading from out[i] instead of out[i - 1] for the parent row.
Follow-up questions
An interviewer at Workday may pivot to one of these next:
- Pascal's Triangle II (LC 119) — only return the kth row, O(k) space.
- Bell triangle.
- Combinations via Pascal (LC 77).
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why fill with 1 first?
Boundaries of every row are 1. Pre-filling saves you from special-casing the first and last column inside the loop.
Can I do this with the binomial formula?
Yes — row[i][j] = C(i, j). But factorials grow fast and modular arithmetic gets fussy. The iterative version is cleaner.
Practice these live with InterviewChamp.AI
Drill Pascal's Triangle and other Workday interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →