Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

GATE CS & DA Preparation Guide  Module 4 – Engineering mathematics (CS section 1; DA sections 1-3)

Graphs, counting, recurrences, generating functions

GATE CS combinatorics by problem family: counting rules, derangements, Catalan numbers, recurrences, generating functions and graph theory.

  • Intermediate
  • 20 minutes
  • Examples run with Python 3.14.8, sympy 1.14.0 and Pyodide 314.0.7
  • By MySmartCoPilot
Free core, Pro depth: 8 of 20 minutes free
Free: Objects into boxes: one decision picks the formula · Four more families, each confirmed by listing small cases · Recurrences: the roots of one equation give the closed form · 2 more sections. With a pass: 2 in-depth parts and 3 interview answers, about 12 minutes.

What you will learn

  • Apply counting rules, inclusion-exclusion, the pigeonhole principle and stars and bars to GATE-style NATs
  • Solve linear recurrences and use generating functions to count
  • Answer graph questions on connectivity, matchings, colouring, planarity and degree sequences

Before you start

Section 1 of the GATE CS syllabus lists graphs (connectivity, matching, colouring) and combinatorics (counting, recurrence relations, generating functions), and the DA paper’s probability section begins with counting, so both papers need the counting half. Counting questions ask for one exact number, the shape of a NAT: Numerical answer type question: no options. The answer is a number typed on the screen's keypad, right when it falls inside the key's range; a wrong one costs nothing. More in “MCQ, MSQ and NAT: GATE marking and when to guess”, which makes them dependable marks once the method is fixed: decide whether the objects and the boxes are distinct and what each box may hold, pick the formula, and check it on a small case by listing. Recurrences reduce to the roots of a characteristic equation: The equation a linear recurrence gives when a(n) = rⁿ is tried: for a(n) = 5a(n − 1) − 6a(n − 2) it is r² − 5r + 6 = 0, and its roots 2 and 3 give a(n) = A · 2ⁿ + B · 3ⁿ.. Generating functions turn a counting question into reading one coefficient of a power series. Questions on graphs rest on a handful of counting facts, the handshake lemma: The degrees of a graph's vertices add up to twice its number of edges, because each edge is counted once at each end. So the number of vertices of odd degree is even. first. This lesson gives the decision diagram, checks six counting families by brute force and solves five problems.

Not affiliated

MySmartCoPilot has no tie to the institutes that conduct GATE. The clauses on graphs and combinatorics come from the official CS and DA syllabus PDFs, which decide what may be asked.

Objects into boxes: one decision picks the formula

Follow a question down the diagram: the kind of objects picks the branch, and what a box may hold picks the formula.

A decision diagram for k objects in n distinct boxes: one set of formulas for distinct objects, another for identical ones.k objects into n distinct boxesObjects distinct?Yes: each object picks a boxNo: only the box sizes matterAny number per boxn^kAt most one per boxn!/(n-k)!No box emptyinclusion-exclusionAny number per boxC(k+n-1, n-1)No box emptyC(k-1, n-1)yesno

Which formula counts k objects in n distinct boxes

Text description of the diagram

The diagram starts from one box at the top, "k objects into n distinct boxes", with an arrow down to the question "Objects distinct?". Two arrows leave the question.

The arrow labelled "yes" leads down to the right, to a group titled "Yes: each object picks a box", with three formulas:

  • any number of objects per box: n^k, because each of the k objects chooses one of n boxes;
  • at most one object per box: n!/(n-k)!, the number of ordered choices of k different boxes;
  • no box empty: inclusion-exclusion, the sum over i of (-1)^i C(n, i) (n - i)^k.

The arrow labelled "no" leads down to the left, to a group titled "No: only the box sizes matter", with two formulas:

  • any number of objects per box: C(k + n - 1, n - 1), the stars-and-bars count;
  • no box empty: C(k - 1, n - 1), stars and bars after putting one object in each box first.

Most counting questions put k objects into n boxes, even when they talk about seats, envelopes or digits. When the objects are distinct, each object chooses its box. When they are identical, only how many land in each box matters, and stars and bars: A count of the ways to put k identical objects into n distinct boxes: k stars and n − 1 bars in a row, so C(k + n − 1, n − 1) ways, or C(k − 1, n − 1) with no box empty. counts the ways: k stars and n − 1 bars in a row. The pigeonhole principle is the same picture read backwards: with n + 1 objects in n boxes, some box holds at least two, as 3 objects in 2 boxes show.

1. Stars and bars (NAT). x₁ + x₂ + x₃ = 10 has C(12, 2) = 66 solutions in non-negative integers and C(9, 2) = 36 in positive integers.

Checkpoint

Check: Objects into boxes

  1. Question 1 of 2 In how many ways can 5 different letters be dropped into 3 different postboxes, any number in each?

    Type a number.

    Show the answer: Question 1 of 2

    Answer: 243 ways

    The letters are distinct and the boxes are distinct, with no limit per box, so each letter chooses one of 3 boxes on its own: 3⁵ = 243.

  2. Question 2 of 2 In how many ways can 10 identical pens be shared among 4 students so that every student gets at least one?

    Type a number.

    A hint

    Give each student one pen first, then share the rest freely.

    Show the answer: Question 2 of 2

    Answer: 84 ways

    Identical objects, and no box empty: hand out one pen each, then share the other 6 among 4 students with stars and bars, C(6 + 3, 3) = C(9, 3) = 84. That is C(k − 1, n − 1) with k = 10 and n = 4, the program's value.

Four more families, each confirmed by listing small cases

  • Arrangements with a restriction: count them all and subtract the bad ones. Four people in a row with two kept apart: 24 − 12 = 12.
  • Derangements: Dₙ = n! (1 − 1/1! + 1/2! − ⋯ ± 1/n!), the orderings that leave nothing in place. D₄ = 9.
  • Catalan numbers: C(2n, n)/(n + 1). For n = 3 there are 5.
  • Inclusion–exclusion: add the single sets, subtract the pairs, add the triples: |A ∪ B| = |A| + |B| − |A ∩ B|.

Catalan numbers count balanced bracket strings, binary search tree shapes and stack-sortable orders, which is why they appear in data-structure questions too.

2. Derangements (NAT). Five letters can go into five addressed envelopes with every letter in a wrong envelope in D₅ = 44 ways.

3. Catalan (NAT). Four distinct keys can form C₄ = C(8, 4)/5 = 14 binary search trees of different shapes.

The program lists every case of six families, these four and stars and bars with and without empty boxes, and compares each count with its formula:

Six counting families: listing every case against the formula Python · count_check.py
from itertools import permutations, product
from math import comb, factorial


def not_adjacent(n):
    """People 0..n-1 in a row, with persons 0 and 1 never side by side."""
    return sum(abs(p.index(0) - p.index(1)) > 1 for p in permutations(range(n)))


def identical_into_boxes(k, n, at_least=0):
    """k identical objects into n distinct boxes: count the tuples of box sizes that add up to k."""
    return sum(sum(c) == k for c in product(range(at_least, k + 1), repeat=n))


def derangements(n):
    """Orderings of 0..n-1 in which no item stays in its own place."""
    return sum(all(p[i] != i for i in range(n)) for p in permutations(range(n)))


def onto(k, n):
    """Functions from k distinct objects onto n distinct boxes (no box empty)."""
    return sum(len(set(f)) == n for f in product(range(n), repeat=k))


def balanced(n):
    """Strings of n '(' and n ')' that never close more brackets than they have opened."""
    count = 0
    for s in product("()", repeat=2 * n):
        depth = 0
        for ch in s:
            depth += 1 if ch == "(" else -1
            if depth < 0:
                break
        count += depth == 0
    return count


def derangements_formula(n):
    return sum((-1) ** i * factorial(n) // factorial(i) for i in range(n + 1))


families = [
    ("row of n, two people apart", [4, 5, 6, 7], not_adjacent, lambda n: factorial(n) - 2 * factorial(n - 1), "n! - 2(n-1)!"),
    ("10 identical into n boxes", [2, 3, 4], lambda n: identical_into_boxes(10, n), lambda n: comb(10 + n - 1, n - 1), "C(10+n-1, n-1)"),
    ("10 identical, none empty", [2, 3, 4], lambda n: identical_into_boxes(10, n, 1), lambda n: comb(9, n - 1), "C(9, n-1)"),
    ("derangements of n", [4, 5, 6, 7], derangements, derangements_formula, "n! (1 - 1/1! + ... )"),
    ("6 distinct onto n boxes", [2, 3, 4], lambda n: onto(6, n), lambda n: sum((-1) ** i * comb(n, i) * (n - i) ** 6 for i in range(n + 1)), "inclusion-exclusion"),
    ("balanced brackets, n pairs", [3, 4, 5, 6, 7], balanced, lambda n: comb(2 * n, n) // (n + 1), "C(2n, n)/(n+1)"),
]
for name, ns, brute, formula, how in families:
    found = [brute(n) for n in ns]
    expected = [formula(n) for n in ns]
    verdict = "agree" if found == expected else "DIFFER"
    print(f"{name:<28} n = {ns}: {found}  {how}: {verdict}")

Output

row of n, two people apart   n = [4, 5, 6, 7]: [12, 72, 480, 3600]  n! - 2(n-1)!: agree
10 identical into n boxes    n = [2, 3, 4]: [11, 66, 286]  C(10+n-1, n-1): agree
10 identical, none empty     n = [2, 3, 4]: [9, 36, 84]  C(9, n-1): agree
derangements of n            n = [4, 5, 6, 7]: [9, 44, 265, 1854]  n! (1 - 1/1! + ... ): agree
6 distinct onto n boxes      n = [2, 3, 4]: [62, 540, 1560]  inclusion-exclusion: agree
balanced brackets, n pairs   n = [3, 4, 5, 6, 7]: [5, 14, 42, 132, 429]  C(2n, n)/(n+1): agree

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 count_check.py

Every family agrees with its formula for every n tried. Listing is slow, but it is exact, and a program that lists the cases for n = 4 is the best check on a formula you are about to use for n = 10.

Permutation & Combination Calculator Check a C(n, k) or P(n, k) value before you build a larger count on it.

Checkpoint

Check: Derangements, Catalan numbers and complements

  1. Question 1 of 2 Three letters go into three addressed envelopes. In how many ways can every letter end up in a wrong envelope?

    Type a number.

    Show the answer: Question 1 of 2

    Answer: 2 ways

    D₃ = 3! (1 − 1 + 1/2 − 1/6) = 6 − 6 + 3 − 1 = 2. With letters a, b and c, the two derangements are b c a and c a b: each moves every letter one place along.

  2. Question 2 of 2 In how many ways can 6 people stand in a row if two particular people must not stand next to each other?

    Type a number.

    A hint

    Count all the rows, then subtract those with the two side by side.

    Show the answer: Question 2 of 2

    Answer: 480 ways

    All rows minus the bad ones: 6! = 720 in all, and 2 · 5! = 240 with the pair glued into one block in either order, so 720 − 240 = 480, the program's value for n = 6.

Recurrences: the roots of one equation give the closed form

A linear recurrence such as a(n) = 5a(n − 1) − 6a(n − 2) is solved by trying a(n) = rⁿ. The characteristic equation r² − 5r + 6 = 0 has roots 2 and 3, so a(n) = A · 2ⁿ + B · 3ⁿ, and the initial values fix A and B.

4. Recurrence (MCQ). With a(0) = 2 and a(1) = 5, A + B = 2 and 2A + 3B = 5 give A = B = 1, so a(n) = 2ⁿ + 3ⁿ and a(5) = 32 + 243 = 275. Step through the same solution:

Interactive · stage by stage Solving a(n) = 5a(n − 1) − 6a(n − 2), a(0) = 2, a(1) = 5 Step through the method. Each step is one move, from the guess rⁿ to the value the question asks for.
Solving a(n) = 5a(n − 1) − 6a(n − 2), a(0) = 2, a(1) = 5, step 6 of 6. Showing Try a(n) = rⁿ, r² − 5r + 6 = 0, Roots 2 and 3, a(n) = A · 2ⁿ + B · 3ⁿ, A + B = 2, 2A + 3B = 5, a(5) = 32 + 243 = 275. In focus: a(5) = 32 + 243 = 275.Try a(n) = rⁿthen divide by rⁿ⁻²r² − 5r + 6 = 0the characteristic equationRoots 2 and 3(r − 2)(r − 3) = 0a(n) = A · 2ⁿ + B · 3ⁿone term for each rootA + B = 2, 2A + 3B = 5so A = 1 and B = 1a(5) = 32 + 243 = 275a(n) = 2ⁿ + 3ⁿ

The drawing shows the whole scene; its 6 steps are listed below.

Every step as text
  1. Guess rⁿ. A linear recurrence with constant coefficients is solved by trying a(n) = rⁿ. Substituting gives rⁿ = 5rⁿ⁻¹ − 6rⁿ⁻², and dividing by rⁿ⁻² leaves an equation in r alone.
  2. The equation in r. That equation, r² − 5r + 6 = 0, is the characteristic equation of the recurrence. Its coefficients 1, −5 and 6 come straight from the recurrence.
  3. Its roots. It factors as (r − 2)(r − 3) = 0, so its roots are 2 and 3. Both 2ⁿ and 3ⁿ satisfy the recurrence.
  4. The general solution. Any combination of the two solutions satisfies the recurrence as well, so for two distinct roots the general solution is a(n) = A · 2ⁿ + B · 3ⁿ.
  5. Fit the initial values. The initial values fix the constants: a(0) = A + B = 2 and a(1) = 2A + 3B = 5. Subtracting twice the first equation from the second gives B = 1, so A = 1.
  6. The value asked for. So a(n) = 2ⁿ + 3ⁿ, and the a(5) the question asks for is 2⁵ + 3⁵ = 32 + 243 = 275.

A generating function stores a sequence as the coefficients of a power series. The ways to choose amounts with a given total are then one coefficient of a product, with one factor per choice.

Checkpoint

Check: Solving linear recurrences

  1. Question 1 of 2 What is the characteristic equation of a(n) = 4a(n − 1) + 5a(n − 2)?

    Choose one answer.

    Show the answer: Question 1 of 2

    Answer: r² − 4r − 5 = 0

    Substitute a(n) = rⁿ and divide by rⁿ⁻²: r² = 4r + 5, so r² − 4r − 5 = 0. It factors as (r − 5)(r + 1), so the general solution is A · 5ⁿ + B · (−1)ⁿ.

  2. Question 2 of 2 a(n) = 5a(n − 1) − 6a(n − 2) with a(0) = 1 and a(1) = 4. What is a(3)?

    Type a number.

    A hint

    The general solution is A · 2ⁿ + B · 3ⁿ. Fit A and B, or unroll two terms.

    Show the answer: Question 2 of 2

    Answer: 46

    A + B = 1 and 2A + 3B = 4 give B = 2 and A = −1, so a(n) = 2 · 3ⁿ − 2ⁿ and a(3) = 54 − 8 = 46. Unrolling agrees: a(2) = 5 · 4 − 6 · 1 = 14 and a(3) = 5 · 14 − 6 · 4 = 46.

Graphs: most questions start from the degrees

For graphs, the facts that settle most questions are short. The degrees add up to twice the number of edges, so the number of odd-degree vertices is even. A connected graph has an Euler circuit exactly when every degree is even. A connected planar graph satisfies v − e + f = 2. There are nⁿ⁻² labelled trees on n vertices.

5. Handshake (NAT). A simple graph with degrees 4, 3, 3, 2 and 2 has 14/2 = 7 edges. Step through one such graph, built an edge at a time:

Interactive · step by step Seven edges and the degrees they make Step through the edges. Each one adds 1 to the degree of both of its ends, so the sum of the degrees grows by 2.
Seven edges and the degrees they make, step 9 of 9. Showing A (degree 4), D (degree 2), B (degree 3), C (degree 3), E (degree 2).Adegree 4Ddegree 2Bdegree 3Cdegree 3Edegree 2

The drawing shows the whole scene; its 9 steps are listed below.

Every step as text
  1. No edges. Five vertices and no edges yet. Every degree is 0, and so is their sum.
  2. Edge 1, A–B. The edge A–B adds 1 to the degree of A and 1 to the degree of B. The sum of the degrees is now 2.
  3. Edge 2, A–C. A–C raises A to 2 and C to 1. Two more for the sum, which is now 4.
  4. Edge 3, A–D. A–D raises A to 3 and D to 1, and the sum to 6.
  5. Edge 4, A–E. A–E gives A its fourth edge and E its first. The sum is 8, twice the 4 edges so far.
  6. Edge 5, B–C. B–C raises B and C to 2 each. The sum is 10.
  7. Edge 6, B–D. B–D raises B to 3 and D to 2. The sum is 12.
  8. Edge 7, C–E. C–E raises C to 3 and E to 2. The degrees are now 4, 3, 3, 2 and 2, and their sum is 14.
  9. Twice the edges. The degrees add up to 14, twice the 7 edges, because every edge was counted once at each end. The two odd degrees, B's 3 and C's 3, come as a pair, as they must.

Each edge added 2 to the sum, once at each end, so the sum of the degrees is always even, and the odd degrees always come in pairs.

Checkpoint

Check: Degrees, Euler circuits and planar graphs

  1. Question 1 of 2 A simple graph has six vertices with the degrees 3, 3, 2, 2, 2 and 2. How many edges does it have?

    Type a number.

    Show the answer: Question 1 of 2

    Answer: 7 edges

    The degrees add up to 3 + 3 + 2 + 2 + 2 + 2 = 14, twice the number of edges, so the graph has 7 edges.

  2. Question 2 of 2 A connected planar graph has 6 vertices and 9 edges. How many faces does a drawing of it without crossings have, the outer face included?

    Type a number.

    Show the answer: Question 2 of 2

    Answer: 5 faces

    Euler's formula for connected planar graphs, v − e + f = 2, gives f = 2 − 6 + 9 = 5. A triangular prism is such a graph: two triangles, three squares between them, and the outer face is one of the triangles.

Practise: counting and graphs in GATE’s formats

Practise all four ideas, starting with the one you know least.

Practice that adapts to you

Practise this lesson

12 questions about 6 ideas of this lesson. Each answer is explained at once, and what you get right is kept in this browser.

  1. Question 1 of 12 In how many ways can 4 different books be put on 3 different shelves, any number per shelf, if the order on a shelf does not matter?

    Type a number.

    Show the answer: Question 1 of 12

    Answer: 81 ways

    Each book chooses one of the 3 shelves, independently: 3⁴ = 81. The books are distinct objects and the shelves distinct boxes, with no limit per box.

  2. Question 2 of 12 In how many ways can 7 identical sweets be shared among 3 children, a child possibly getting none?

    Type a number.

    Show the answer: Question 2 of 12

    Answer: 36 ways

    Stars and bars: 7 stars and 2 bars in a row, C(9, 2) = 36. The sweets are identical, so only how many each child gets matters.

  3. Question 3 of 12 In how many ways can 7 identical sweets be shared among 3 children so that every child gets at least one?

    Type a number.

    Show the answer: Question 3 of 12

    Answer: 15 ways

    Give one sweet to each child first, then share the remaining 4 freely: C(4 + 2, 2) = C(6, 2) = 15, which is also C(7 − 1, 3 − 1).

  4. Question 4 of 12 In how many ways can 5 people stand in a row if two particular people must not stand next to each other?

    Type a number.

    Show the answer: Question 4 of 12

    Answer: 72 ways

    All orders minus the bad ones: 5! = 120 in all, and 2 · 4! = 48 with the pair glued together (4 blocks, and the pair in either order). 120 − 48 = 72, as the brute force in the lesson lists.

  5. Question 5 of 12 Four people each bring a gift and every person takes home someone else's. In how many ways can this happen?

    Type a number.

    Show the answer: Question 5 of 12

    Answer: 9 ways

    This is a derangement of 4 items: 4! (1 − 1 + 1/2 − 1/6 + 1/24) = 24 − 24 + 12 − 4 + 1 = 9.

  6. Question 6 of 12 For n = 3, which of these counts equal the Catalan number C₃ = 5?

    Choose every answer that is right.

    Show the answer: Question 6 of 12

    Answer:

    • Shapes of binary search trees with 3 distinct keys
    • Balanced strings of 3 pairs of brackets

    Bracket strings and binary search tree shapes are Catalan objects: 5 each for n = 3. There are 3! = 6 orderings and 2³ = 8 subsets.

  7. Question 7 of 12 What is the general solution of a(n) = 7a(n − 1) − 12a(n − 2)?

    Choose one answer.

    Show the answer: Question 7 of 12

    Answer: A · 3ⁿ + B · 4ⁿ

    Try a(n) = rⁿ: r² = 7r − 12, so r² − 7r + 12 = (r − 3)(r − 4) = 0. Two distinct roots give A · 3ⁿ + B · 4ⁿ.

  8. Question 8 of 12 What is the smallest number of cards you must draw from a standard 52-card deck to be sure that two have the same suit?

    Type a number.

    Show the answer: Question 8 of 12

    Answer: 5 cards

    Four suits are four boxes. Four cards can all differ in suit, but the fifth must repeat one: n + 1 objects in n boxes.

  9. Question 9 of 12 A graph has 7 vertices. Which of these could be the number of vertices of odd degree?

    Choose one answer.

    Show the answer: Question 9 of 12

    Answer: 6

    The degrees add up to twice the number of edges, an even number, so the number of odd-degree vertices is even. Only 6 is even.

  10. Question 10 of 12 Which of these connected graphs have an Euler circuit, a closed walk that uses every edge exactly once?

    Choose every answer that is right.

    Show the answer: Question 10 of 12

    Answer:

    • A cycle on 5 vertices
    • The complete graph K₅

    A connected graph has an Euler circuit exactly when every degree is even. The 5-cycle has degrees 2 and K₅ has degrees 4. K₄ has degrees 3, and a path has two ends of degree 1.

  11. Question 11 of 12 Each of 5 friends picks one of 3 films. The cinema records only how many tickets each film sells. How many different records are possible?

    Choose one answer.

    Show the answer: Question 11 of 12

    Answer: 21

    The friends are distinct people, but the record forgets who chose what, so only the box sizes matter: 5 stars and 2 bars, C(7, 2) = 21. 3⁵ = 243 would count each friend's choice.

  12. Question 12 of 12 At a party of 9 people, can each person shake hands with exactly 3 of the others?

    Choose one answer.

    Show the answer: Question 12 of 12

    Answer: No, because 9 × 3 = 27 is odd, and the sum of the degrees is always even

    Read people as vertices and handshakes as edges. The degrees would add up to 9 × 3 = 27, but every edge adds 2 to the sum, so the sum is even. No such party exists.

Counting and recurrences: each formula derived and checked by program Pro: opens with a Pro, Premium or Ultimate pass

Every formula in this module comes from a few arguments, and knowing them is what saves a question whose wording hides the family. This part derives them, solves three recurrences three ways and reads counts off generating functions.

In this part:

  • Inclusion–exclusion worked: onto maps and derangements from one formula
  • Catalan numbers: three objects, one count, worked to n = 5
  • Recurrences solved three ways, checked against SymPy
  • Generating functions: reading a coefficient, checked three ways
  • Count k objects in n boxes for all four cases
    • Results of the sample tests

2 runnable examples (Python) · 1 exercise with sample tests · about 5 minutes

Opening this part with your pass…

This part opens with any MySmartCoPilot pass.

  • Pro pass · 1 week₹79$2.99
  • 30 days₹179$5.99
  • 1 year₹1,699$45

One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.

It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).

Graph questions: bounds, degree sequences, colouring and planarity checked Pro: opens with a Pro, Premium or Ultimate pass

Many graph questions are counting questions in disguise, and every bound they use can be checked on small graphs. This part counts all graphs on five labelled vertices, tests degree sequences and colourings by program, and ends with a ten-minute set with answers.

In this part:

  • All 1,024 graphs on five labelled vertices, counted
  • Degree sequences, colourings and planarity, tested
  • Matchings: Hall’s condition, with a failing case worked
  • A ten-minute set, with answers

1 diagram · 2 runnable examples (Python) · about 6 minutes

Opening this part with your pass…

This part opens with any MySmartCoPilot pass.

  • Pro pass · 1 week₹79$2.99
  • 30 days₹179$5.99
  • 1 year₹1,699$45

One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.

It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).

Interview questions

  1. Fresher · Warm-up

    When do you count placements with n to the power k, and when with stars and bars?

    Both count k objects in n distinct boxes; the difference is whether the objects can be told apart. If they are distinct, such as people choosing hostels, each object chooses its box on its own, which gives n to the power k. If they are identical, such as sweets or marks to distribute, two placements with the same number in every box are the same placement, and stars and bars counts them: k stars and n − 1 bars in a row, C(k + n − 1, n − 1). For 4 objects and 3 boxes that is 81 against 15. When every box must get something, the identical case becomes C(k − 1, n − 1), and the distinct case needs inclusion–exclusion.

  2. Fresher to mid

    A simple graph has 20 vertices and exactly 3 connected components. What is the largest number of edges it can have, and how do you show that no graph does better?

    Model answer, Pro. Model answers open with any pass; prices are in the notice on this page. First step and follow-ups

  3. Fresher to mid

    A NAT gives a(n) = 6a(n − 1) − 9a(n − 2) with a(0) = 2 and a(1) = 3 and asks for a(5). A candidate writes a(n) = A · 3ⁿ + B · 3ⁿ. What goes wrong, what is the right form, and how do you check the value?

    Model answer, Pro. First step and follow-ups

  4. Fresher to mid

    How many bit strings of length 10 contain no two consecutive 1s? Derive the count in a way that would also handle "no three consecutive 1s".

    Model answer, Pro. First step and follow-ups

Key takeaways

  • Decide first whether the objects and the boxes are distinct and what a box may hold; the formula follows, and a listing for n = 4 confirms it.
  • Counting by complement, stars and bars, derangements and Catalan numbers are the families to recognise first.
  • Linear recurrences reduce to the roots of their characteristic equation; generating functions reduce counting to reading a coefficient.
  • For graphs, start from the handshake lemma and the parity of the degrees.

Terms in this lesson

characteristic equation
The equation a linear recurrence gives when a(n) = rⁿ is tried: for a(n) = 5a(n − 1) − 6a(n − 2) it is r² − 5r + 6 = 0, and its roots 2 and 3 give a(n) = A · 2ⁿ + B · 3ⁿ.
handshake lemma
The degrees of a graph's vertices add up to twice its number of edges, because each edge is counted once at each end. So the number of vertices of odd degree is even.
stars and bars
A count of the ways to put k identical objects into n distinct boxes: k stars and n − 1 bars in a row, so C(k + n − 1, n − 1) ways, or C(k − 1, n − 1) with no box empty.

References

Related tools

Report a problem with this lesson

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.