Skip to main content

15. Pascal's Triangle

easyAsked at Plaid

Generate the first numRows of Pascal's triangle. Plaid asks this as a DP-warm-up that exercises the 'each row depends on the previous' pattern used in their rolling-window financial aggregations.

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

Source citations

Public interview reports confirming this problem appears in Plaid loops.

  • Glassdoor (2025)Plaid intern OA.
  • LeetCode Discuss (2026)Plaid warm-up.

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

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. Compute each entry via C(n,k) formula

Use binomial coefficient n!/(k!(n-k)!).

Time
O(n^2)
Space
O(n^2)
function generate(numRows) {
  const rows = [];
  for (let n = 0; n < numRows; n++) {
    const row = [];
    let c = 1;
    for (let k = 0; k <= n; k++) {
      row.push(c);
      c = c * (n - k) / (k + 1);
    }
    rows.push(row);
  }
  return rows;
}

Tradeoff: Works for small n. Floating-point division could fail for large n but stays exact for numRows <= 30.

2. Build row from previous row

Each row starts and ends with 1. Middle entries are sum of two adjacent entries from the previous row.

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

Tradeoff: Pure integer arithmetic, no floats. The 'depends on previous row' pattern is exactly the rolling-DP shape Plaid wants you to recognize.

Plaid-specific tips

Plaid uses Pascal's triangle to verify you reach for 'each row from the previous' before doing closed-form math. Bonus signal: explicitly call out that this is the same DP pattern as a running 30-day window over a transaction stream.

Common mistakes

  • Indexing into prev[j+1] instead of prev[j] when computing the middle entries.
  • Forgetting to push the trailing 1.
  • Using floats when integer arithmetic suffices — risks precision drift.

Follow-up questions

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

  • Pascal's Triangle II (LC 119) — return the kth row using O(k) space.
  • Compute C(n, k) directly without building the triangle.
  • What if you only need diagonal entries? Same loop with different indexing.

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 two 1s at the edges?

C(n, 0) = C(n, n) = 1 by definition. The interior follows the Pascal recurrence.

Can you compute a single row in O(k) space?

Yes — fill row[k] left-to-right using the previous value of row[k-1]. That's LC 119.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →