Combinatorics
Interview prep for combinatorics including permutations, combinations, Catalan numbers, and advanced counting techniques
Fundamental Concepts
Basic Counting Principles
Addition Principle: If there are ways to do task A and ways to do task B, and these tasks are mutually exclusive, then there are ways to do either task A or task B.
Multiplication Principle: If there are ways to do task A and ways to do task B, then there are ways to do both tasks in sequence.
Subtraction Principle: If there are ways to do a task without restrictions and ways that violate certain restrictions, then there are ways to do the task satisfying the restrictions.
Division Principle: If there are ways to perform a task, but each outcome is counted times due to symmetry, then there are distinct outcomes.
Permutations
Permutations of n distinct objects:
Permutations of r objects from n distinct objects (order matters):
Permutations with repetition: Arranging objects where there are of type 1, of type 2, ..., of type k:
Combinations
Combinations of r objects from n (order doesn't matter):
Properties:
- (symmetry)
- (Pascal's identity)
- (sum of binomial coefficients)
Stars and Bars
Problem: Distribute identical objects into distinct bins.
Non-negative integers (bins can be empty):
Positive integers (each bin must have at least one):
Intuition: Stars represent objects, bars separate bins. For stars and bars, choose positions for the bars.
Essential Identities and Formulas
Binomial Theorem
Special cases:
- (for )
Multinomial Theorem
where the sum is over all non-negative integers , and:
Vandermonde's Identity
Interpretation: Choosing objects from two groups of sizes and .
Hockey Stick Identity
Interpretation: Sum of diagonal entries in Pascal's triangle.
The 12-Fold Way
Counting the number of ways to place balls into bins:
| Balls | Bins | Any arrangement | Injective (≤1 per bin) | Surjective (≥1 per bin) |
|---|---|---|---|---|
| Distinguishable | Distinguishable | |||
| Indistinguishable | Distinguishable | |||
| Distinguishable | Indistinguishable | if , else | ||
| Indistinguishable | Indistinguishable | if , else |
Notation:
- is the falling factorial:
- is the Stirling number of the second kind (partitions of into non-empty subsets)
- is the partition function (partitions of into at most parts)
Stirling Numbers of the Second Kind
Number of ways to partition objects into non-empty subsets:
Recurrence:
Boundary conditions: for , ,
Catalan Numbers
Definition
The -th Catalan number is:
First few values:
Recurrence relation:
with .
Generating function:
Applications of Catalan Numbers
- Number of valid parenthesizations of pairs of parentheses
- Number of binary trees with internal nodes
- Number of ways to triangulate a convex -gon
- Number of monotonic lattice paths from to that don't cross the diagonal (Dyck paths)
- Number of ways to connect points on a circle with non-crossing chords
- Number of permutations of that avoid the pattern (or any pattern of length 3)
Dyck Paths and Lattice Path Counting
Dyck Paths
A Dyck path is a lattice path from to using steps (right) and (up) that never goes above the diagonal .
Count: The number of Dyck paths from to is (the -th Catalan number).
Proof technique: Reflection principle (cycle lemma).
General Lattice Path Counting
Problem: Count paths from to using right and up steps.
Answer:
Intuition: Total steps = , choose which are "right" (or which are "up").
Lattice Paths with Barriers
Problem: Count paths from to that don't touch or cross a barrier.
Technique: Reflection principle (also called the ballot problem or cycle lemma).
Example: Paths from to that stay strictly below the diagonal:
Ballot Problem
In an election, candidate A receives votes and candidate B receives votes, with . The number of ways the ballots can be ordered such that A is always ahead is:
Inclusion-Exclusion Principle
General Formula
For events :
Compact form:
Derangements
A derangement is a permutation where no element appears in its original position.
Number of derangements of objects:
Approximation:
Recurrence:
with .
Probability: The probability that a random permutation is a derangement approaches as .
Burnside's Lemma (Pólya Enumeration)
Burnside's Lemma
Problem: Count distinct objects under group symmetries.
Formula: Let be a group acting on a set . The number of distinct objects (orbits) is:
where is the set of elements fixed by symmetry .
Intuition: Average number of elements fixed by each symmetry.
Common Applications
Necklace problem: Count distinct necklaces with beads and colors, considering rotations and reflections as equivalent.
Cube coloring: Count ways to color the faces of a cube with colors, considering rotational symmetries.
Steps:
- Identify the symmetry group
- For each symmetry , count how many configurations are fixed by
- Apply Burnside's lemma
Example: Rotating a Square
For a square with positions and colors:
- Identity: configurations fixed
- rotation: configurations fixed (all same color in each "orbit")
- rotation: configurations fixed
- rotation: configurations fixed
Total distinct configurations:
Grid Path Counting
Basic Grid Paths
From to using right and up steps:
Paths Avoiding Obstacles
Technique: Inclusion-exclusion or dynamic programming.
Single obstacle at :
Paths on a Grid with Boundaries
Rectangle with barrier: Use reflection principle.
Cycle lemma / André's reflection: For paths from to that stay weakly below :
Diagonal Paths
Paths using diagonal steps: If diagonal is allowed along with and :
Count using generating functions or recurrence relations based on last step.
Counting on Strings
Binary Strings
Binary strings of length :
Binary strings with exactly ones:
Binary strings avoiding pattern: Use recurrence relations or generating functions.
Avoiding Consecutive Patterns
Binary strings of length with no two consecutive 1s:
where is the -th Fibonacci number.
Recurrence: with .
Strings with Forbidden Substrings
Technique: Inclusion-exclusion or automata/dynamic programming.
Example: Strings of length over alphabet avoiding substring :
- Build a DFA/automaton that accepts strings containing
- Use DP or transfer matrix method to count
Domino Tilings
Tiling a Board
Problem: Count ways to tile a board with dominoes.
Answer: (Fibonacci number)
Recurrence: with .
Intuition: Last column is either a vertical domino or two horizontal dominoes.
Tiling an Board
For general boards:
- Use transfer matrix method
- For small , recurrence relations work
- For large boards, use advanced techniques (e.g., Kasteleyn's theorem for exact formula)
Aztec Diamond
The Aztec diamond of order has exactly domino tilings.
Generating Functions
Ordinary Generating Functions (OGF)
For sequence :
Operations:
- Addition:
- Multiplication: gives convolution
- Shift: shifts sequence by one
Common OGFs:
- (geometric series)
- (stars and bars)
- (binomial)
Exponential Generating Functions (EGF)
For sequence :
Use case: When order matters (permutations rather than combinations).
Example: Number of permutations of objects:
corresponds to .
Advanced Counting Techniques
Recurrence Relations
Linear recurrence:
Solution technique:
- Find characteristic equation:
- Solve for roots
- General solution is linear combination of for each root
Example (Fibonacci): has characteristic equation , giving:
where and .
Bijective Proofs
Technique: Establish a one-to-one correspondence between two sets to show they have the same size.
Example: by mapping a subset to its complement .
Double Counting
Technique: Count the same quantity in two different ways to establish an identity.
Example: Handshaking lemma:
Pigeonhole Principle
Basic Pigeonhole Principle
If pigeons are placed in holes and , then at least one hole contains more than one pigeon.
Generalized: If objects are placed in bins, at least one bin contains at least objects.
Applications
- Ramsey theory (finding structure in large sets)
- Proving existence of patterns
- Subset sum problems
Example: Among any integers, there exist two whose difference is divisible by .
Common Combinatorial Tricks
Trick 1: Complementary Counting
Count the complement and subtract:
When to use: When the complement is easier to count.
Trick 2: Bijection Construction
Establish a one-to-one mapping between two sets.
Example: Counting Dyck paths ↔ valid parenthesizations.
Trick 3: Stars and Bars
Convert distribution problems to choosing positions for dividers.
When to use: Distributing identical objects into distinct bins.
Trick 4: Recursive Decomposition
Break problem into smaller subproblems based on choices.
Example: Catalan numbers—split based on where the first matching parenthesis closes.
Trick 5: Generating Function Manipulation
Encode the problem as a generating function and use algebra.
When to use: Recurrences, convolutions, coefficient extraction.
Trick 6: Symmetry Arguments
Use symmetry to reduce cases or simplify counting.
Example: Rotating/reflecting objects, choosing from vs. choosing from .
Trick 7: Inclusion-Exclusion
Add and subtract overlapping sets to avoid double-counting.
When to use: At-least-one problems, derangements, forbidden configurations.
Trick 8: Reflection Principle
Reflect "bad" paths across a boundary to count them.
When to use: Lattice paths with barriers, ballot problems.
Trick 9: DP State Compression
For grid/path problems, track only necessary state.
Example: Domino tilings—track configuration of last column.
Trick 10: Cycle Index (Pólya Theory)
Extend Burnside's lemma with cycle structure.
When to use: Advanced symmetry problems, weighted counting.
Interview Problem Types
Type 1: Basic Counting (Permutations/Combinations)
| Given | Find | Approach |
|---|---|---|
| Selection or arrangement of distinct objects | Number of ways | Use for selection, for arrangement |
Type 2: Stars and Bars Problems
| Given | Find | Approach |
|---|---|---|
| Distributing identical objects into distinct bins | Number of distributions | Use (bins can be empty) or (bins non-empty) |
Type 3: Lattice Path Counting
| Given | Find | Approach |
|---|---|---|
| Grid paths from to with constraints | Number of valid paths | Use baseline, reflection principle for barriers |
Type 4: Catalan Number Applications
| Given | Find | Approach |
|---|---|---|
| Parenthesizations, Dyck paths, binary trees, triangulations | Count of valid structures | Recognize Catalan pattern: |
Type 5: Inclusion-Exclusion Problems
| Given | Find | Approach |
|---|---|---|
| Count with forbidden overlaps (derangements, at-least-one) | Objects satisfying constraints | Use |
Type 6: String Counting with Constraints
| Given | Find | Approach |
|---|---|---|
| Strings over alphabet avoiding patterns | Number of valid strings | Recurrence relation, DP, or generating functions |
Type 7: Symmetry and Burnside's Lemma
| Given | Find | Approach |
|---|---|---|
| Count distinct objects under symmetries (rotations/reflections) | Number of equivalence classes | Apply Burnside: |
Type 8: Tiling and Domino Problems
| Given | Find | Approach |
|---|---|---|
| Tiling boards with dominoes or polyominoes | Number of tilings | Recurrence relations (often Fibonacci-like), transfer matrices |
Type 9: Stirling Numbers and Partitions
| Given | Find | Approach |
|---|---|---|
| Partition objects into groups, distribute balls into bins | Count under 12-fold way | Use Stirling or partition function |
Type 10: Generating Function Problems
| Given | Find | Approach |
|---|---|---|
| Recurrence or sequence with generating function | Closed form or -th term | Set up OGF/EGF, manipulate algebraically, extract coefficients |
Common Pitfalls
Pitfall 1: Confusing Permutations and Combinations
Wrong: Using when order matters
Check: Does rearranging the selection give a different outcome?
Pitfall 2: Forgetting the Division Principle
Wrong: Overcounting symmetric configurations
Example: Circular arrangements—divide by for rotations
Pitfall 3: Misapplying Stars and Bars
Wrong: Using wrong formula for "at least one in each bin"
Check: Empty bins allowed? Use . Non-empty? Use
Pitfall 4: Off-by-One Errors in Paths
Wrong: Miscounting steps or boundary conditions
Check: Verify small cases, ensure start and end points are correct
Pitfall 5: Incorrect Inclusion-Exclusion
Wrong: Missing terms or wrong signs in inclusion-exclusion
Check: Alternating signs, all subsets included
Pitfall 6: Not Recognizing Catalan Patterns
Wrong: Solving from scratch when pattern is Catalan
Check: Does problem involve balanced/nested structures, non-crossing partitions, or Dyck paths?
Quick Reference: Key Formulas
Permutations:
Combinations:
Stars and Bars:
Catalan Numbers:
Derangements:
Grid Paths:
Burnside's Lemma:
Fibonacci Recurrence:
Stirling Numbers (2nd kind):
Practice Problem Categories
- Combinations with repetition
- Circular permutations
- Multinomial coefficients
- Partitions of integers
- Bell numbers
- Fibonacci variants
- Pascal's triangle identities
- Binomial coefficient identities
- Lattice paths with multiple barriers
- Non-crossing partitions
- Parking functions
- Young tableaux
- Plane partitions
- Schröder numbers
- Motzkin numbers
- Graph coloring under symmetry
- Necklace and bracelet problems