Skip to main content

14. Pascal's Triangle

easyAsked at Adobe

Generate the first numRows rows of Pascal's triangle as a list of lists. Adobe uses this to assess 2D array construction skills and the candidate's ability to build each row from the previous one — a pattern that mirrors incremental rendering pipelines.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Adobe loops.

  • Glassdoor (2025-09)Adobe junior-track candidates report array-construction problems including Pascal's triangle.
  • Reddit r/cscareerquestions (2025-11)Adobe university-hire interviewers ask iterative 2D array problems as warm-ups.

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. The first and last element of each row are always 1.

Constraints

  • 1 <= numRows <= 30

Examples

Example 1

Input
numRows = 5
Output
[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]

Example 2

Input
numRows = 1
Output
[[1]]

Approaches

1. Brute force — recompute from scratch per row

For each row, create an array of 1s and fill the interior by looking up the previous row each time.

Time
O(numRows^2)
Space
O(numRows^2)
function generate(numRows) {
  const result = [];
  for (let i = 0; i < numRows; i++) {
    const row = Array(i + 1).fill(1);
    for (let j = 1; j < i; j++) {
      row[j] = result[i-1][j-1] + result[i-1][j];
    }
    result.push(row);
  }
  return result;
}

Tradeoff: This is already the optimal approach — there's no way around O(numRows^2) output size. The brute force and optimal are the same complexity.

2. Iterative with explicit row construction

Build each row by starting with [1], then computing interior values from the previous row, and appending a final 1. This is clean and makes the boundary conditions explicit, which Adobe interviewers appreciate.

Time
O(numRows^2)
Space
O(numRows^2)
function generate(numRows) {
  const triangle = [[1]];
  for (let i = 1; i < numRows; i++) {
    const prev = triangle[i - 1];
    const row = [1];
    for (let j = 1; j < i; j++) {
      row.push(prev[j - 1] + prev[j]);
    }
    row.push(1);
    triangle.push(row);
  }
  return triangle;
}

Tradeoff: Explicit boundary handling makes this easier to reason about and extend. Adobe values readable code over clever one-liners for this class of problem.

Adobe-specific tips

Adobe interviewers often extend this to 'given row k, return only that row without building the full triangle' — requiring the candidate to recognize the O(k) space solution using the previous row only. Also be prepared to discuss how binomial coefficients relate to image convolution kernels (Gaussian blur approximation).

Common mistakes

  • Off-by-one on the inner loop boundary — the interior loop runs from index 1 to i-1 (exclusive), not 0 to i.
  • Mutating the previous row in place rather than building a fresh row.
  • Forgetting the trailing 1 at the end of each row.

Follow-up questions

An interviewer at Adobe may pivot to one of these next:

  • Pascal's Triangle II (LC 119): return only the kth row using O(k) space.
  • How would you use Pascal's triangle to compute C(n, k) efficiently?
  • How does the convolution kernel in image blurring relate to Pascal's triangle rows?

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why is the time complexity O(numRows^2)?

Each row i has i+1 elements, so the total elements across all rows is 1+2+...+numRows = numRows*(numRows+1)/2, which is O(numRows^2).

Can I build this in O(k) space for just one row?

Yes — iterate from right to left updating a single array in-place. row[j] = row[j] + row[j-1] for j from i down to 1. This avoids needing a second array.

Practice these live with InterviewChamp.AI

Drill Pascal's Triangle and other Adobe interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →