Skip to main content

16. Pascal's Triangle

easyAsked at Coinbase

Generate the first numRows of Pascal's triangle. Coinbase asks this to test array-building discipline and to set up follow-ups on combinatorics (used in risk-pricing).

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

Source citations

Public interview reports confirming this problem appears in Coinbase loops.

  • Glassdoor (2026-Q1)Coinbase warm-up before binomial-coefficient discussion.

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. Binomial coefficient per cell

For each (n, k), compute n!/(k!(n-k)!).

Time
O(n^3)
Space
O(n^2)
function generate(numRows) {
  function C(n, k) { if (k < 0 || k > n) return 0; let r = 1; for (let i = 0; i < k; i++) r = (r * (n - i)) / (i + 1); return r; }
  const out = [];
  for (let i = 0; i < numRows; i++) {
    const row = [];
    for (let j = 0; j <= i; j++) row.push(C(i, j));
    out.push(row);
  }
  return out;
}

Tradeoff: Wastes work and may lose precision via float division. Use the recurrence.

2. Build rows from previous

Row i has length i+1. row[0] = row[i] = 1; row[j] = prev[j-1] + prev[j] for inner cells.

Time
O(n^2)
Space
O(n^2) for 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: Integer-only arithmetic, O(n^2). The recurrence is the textbook approach.

Coinbase-specific tips

Coinbase grades this on whether you avoid floating-point division. Mention that the binomial-coefficient form requires integer division that's easy to get wrong; the additive recurrence is float-safe. They sometimes follow with 'compute C(n, k) mod p' for large n.

Common mistakes

  • Floating-point in the binomial formula — accumulating errors for large rows.
  • Off-by-one in the inner loop — only inner cells get the sum; the endpoints are 1.
  • Allocating each row as [], then push — using new Array(i+1).fill(1) is cleaner.

Follow-up questions

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

  • Return only the kth row (LC 119) using O(k) space.
  • Compute C(n, k) mod p for n up to 10^9.
  • Pascal's triangle along the diagonals — Fibonacci appearance.

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

When would floating-point be safe?

Practically never for combinatorics. Past ~15 rows you start losing precision; for crypto-domain math always use integers or BigInt.

Can I do this with O(n) extra space?

Only if you need a single row (LC 119). For the whole triangle, the output itself is O(n^2) — that's the lower bound.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →