Number Patterns and Sequences Explained — Fibonacci, Catalan, Triangular for Interviews [LC 96, Google, Meta]
Advertisement
Algorithm Statement
A reference of the most interview-relevant integer sequences and their identities:
- Fibonacci: , , .
- Triangular: .
- Catalan: , with the recurrence .
- Lucas: , , .
- Pell: , , .
Constraints typical:
- for direct DP.
- for matrix exponentiation or closed forms.
Examples:
F(10) = 55
T(10) = 55
C(5) = 42 (LC 96 BST count for n = 5)
L(10) = 123Why This Problem Matters
Interview problems are often disguised classical sequences. LC 96 Unique Binary Search Trees is the Catalan number. LC 70 Climbing Stairs is Fibonacci. LC 22 Generate Parentheses is enumeration of Catalan structures. LC 1359 Count All Valid Pickup and Delivery Options is double factorial / Catalan. Recognising the pattern saves you 20 minutes of derivation.
Google and Meta especially reward candidates who say "this is the -th Catalan number, so the answer is " within a minute of seeing the problem. It signals breadth — and avoids the brittle DP you would otherwise write under pressure.
The Core Insight
Fibonacci counts climbing-stairs paths, tilings of a board with dominoes, and binary strings of length with no two consecutive 1s. Closed form: where . For up to , use matrix exponentiation.
Triangular numbers count the number of unordered pairs from items, hence . They satisfy . Useful for problems like "how many handshakes among people".
Catalan numbers count an explosive number of structures: balanced parentheses, BSTs on nodes, monotone lattice paths under the diagonal, full binary trees with leaves, triangulations of a convex -gon, non-crossing chord diagrams. The unifying identity:
Closed form: .
Lucas numbers are the "twin" of Fibonacci with a different start. Identity: .
Pell numbers count perfect squares that are also triangular and appear in continued-fraction expansions of .
Why Catalan equals . Reflection principle: among the lattice paths from to , exactly cross the diagonal. The valid count is .
Visual Dry Run
Catalan number for via the recurrence.
C(0) = 1
C(1) = C(0)*C(0) = 1
C(2) = C(0)*C(1) + C(1)*C(0) = 1 + 1 = 2
C(3) = C(0)*C(2) + C(1)*C(1) + C(2)*C(0) = 2 + 1 + 2 = 5
C(4) = C(0)*C(3) + C(1)*C(2) + C(2)*C(1) + C(3)*C(0)
= 5 + 2 + 2 + 5 = 14
Verify with closed form: C(4) = (1/5) * binomial(8, 4)
= (1/5) * 70 = 14.LC 96 with : count BSTs on 3 nodes.
Pick root r in {1, 2, 3}.
r = 1: left subtree empty, right has {2, 3} → 1 * C(2) = 2
r = 2: left has {1}, right has {3} → 1 * 1 = 1
r = 3: left has {1, 2}, right empty → C(2) * 1 = 2
Total = 5 = C(3). Matches.Triangular number example.
T(5) = 5*6/2 = 15.
Visual: dot triangle with rows of 1, 2, 3, 4, 5 dots → total 15.Solution (Optimal)
Python — Direct Sequences
# Fibonacci O(n) DP
def fib(n: int) -> int:
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return a
# Triangular closed form
def triangular(n: int) -> int:
return n * (n + 1) // 2
# Catalan via DP O(n^2)
def catalan_dp(n: int) -> int:
C = [0] * (n + 1)
C[0] = 1
for i in range(1, n + 1):
C[i] = sum(C[j] * C[i - 1 - j] for j in range(i))
return C[n]
# Catalan via closed form O(n)
from math import comb
def catalan(n: int) -> int:
return comb(2 * n, n) // (n + 1)Python — Catalan Modulo Prime
MOD = 10**9 + 7
def catalan_mod(n: int) -> int:
# Precompute factorials in O(n)
fact = [1] * (2 * n + 2)
for i in range(1, len(fact)):
fact[i] = fact[i - 1] * i % MOD
inv = pow(fact[2 * n], MOD - 2, MOD)
binom = fact[2 * n] * pow(fact[n], MOD - 2, MOD) % MOD \
* pow(fact[n], MOD - 2, MOD) % MOD
return binom * pow(n + 1, MOD - 2, MOD) % MODComplexity: time per query (after precomputing factorials), memory.
JavaScript
function fib(n) {
let [a, b] = [0n, 1n];
for (let i = 0; i < n; i++) [a, b] = [b, a + b];
return a;
}
function triangular(n) {
return BigInt(n) * BigInt(n + 1) / 2n;
}
function catalan(n) {
let C = 1n;
for (let i = 0; i < n; i++)
C = C * BigInt(2 * (2 * i + 1)) / BigInt(i + 2);
return C;
}Common Mistakes
- Computing Catalan with double factorials and floats. Use exact integer arithmetic. The intermediate overflows 64 bits past .
- Wrong base case for Fibonacci. Some sources use , others . Confirm with the interviewer.
- Triangular overflow. overflows 32-bit ints for . Promote to 64-bit before the multiply.
- Missing the modular inverse for Catalan. When the problem asks for the answer mod prime, compute then multiply by the inverse of .
- Confusing zero-indexed and one-indexed Catalan. counts the empty structure. counts a single node.
- Reaching for matrix exponentiation when DP suffices. For the simple DP is faster and clearer than the matrix path.
Interview Tips
- Recite the Catalan list quickly: 1, 1, 2, 5, 14, 42, 132, 429. If you see 14 or 42 in an example, scream "Catalan!"
- For "count valid parentheses" or "count BSTs", state the closed form before any code.
- For Fibonacci with huge, immediately mention matrix exponentiation; for medium, just do the iterative DP.
- Mention the bijection between Catalan structures (parens, BSTs, paths) — interviewers love structural arguments.
Follow-up Questions
Q1: Compute for . A: Matrix exponentiation in using the Fibonacci matrix.
Q2: Why does the same Catalan number count BSTs and balanced parens? A: Build a bijection: in-order traversal of a BST with nodes yields a sequence of "open" and "close" events that form a balanced parenthesis string of length .
Q3: What is the asymptotic growth of ? A: (Stirling's approximation).
Q4: Generalised Catalan: count paths from to that never go above the diagonal. A: Ballot problem; the count is for .
Key Takeaways
- Fibonacci counts staircase paths and tilings; closed form , matrix form for .
- Triangular numbers are — the answer to nearly every "handshake" or "pair" question.
- Catalan numbers count balanced parens, BSTs, full binary trees, lattice paths, and triangulations; closed form .
- The Catalan recurrence comes from picking the root (or first matched paren) and recursing on left and right halves.
- For modular Catalan, precompute factorials and inverse factorials, then assemble the closed form.
- Recognising the sequence on sight saves 20 minutes of DP derivation — memorise the first ten Catalan terms.
Advertisement