CS Atlas GATE 2027
TOC course
0 / 91 complete

TOC/Phase 1 · Finite Automata

Lesson 1.1.3 · Item 17 of 91

DFA design patterns

Read at your own pace. Try the questions before opening the answers.

On this page

Recall warm-up

  1. When does a DFA accept ε?
    Stuck? Peek at 1.1.1 States and the DFA
    • DFA = (Q, Σ, δ, q₀, F), δ : Q × Σ → Q total. One step per symbol; always halts.
    • Accept iff the last state ∈ F. ε accepted ⇔ q₀ ∈ F.
    • Dead state: non-accepting, all self-loops. GATE counts it in minimal-DFA questions.
    • Exactly one string of length k: k + 2 states. #DFAs (n states, m symbols, fixed start) = nⁿᵐ · 2ⁿ.

    Read lesson 1.1.1

  2. How many DFAs with 2 states over {a,b} with fixed start?
    Stuck? Peek at 1.1.1 States and the DFA
    • DFA = (Q, Σ, δ, q₀, F), δ : Q × Σ → Q total. One step per symbol; always halts.
    • Accept iff the last state ∈ F. ε accepted ⇔ q₀ ∈ F.
    • Dead state: non-accepting, all self-loops. GATE counts it in minimal-DFA questions.
    • Exactly one string of length k: k + 2 states. #DFAs (n states, m symbols, fixed start) = nⁿᵐ · 2ⁿ.

    Read lesson 1.1.1

  3. If 11 numbers are divided by 5, must two share a remainder?
    Stuck? Peek at 0.2.4 Pigeonhole principle
    • n + 1 objects in n boxes ⇒ some box has ≥ 2.
    • N objects in k boxes ⇒ some box has ≥ ⌈N/k⌉. To force m in a box: (m−1)k + 1 objects.
    • Machine with n modes, input length ≥ n ⇒ some mode repeats ⇒ a loop in the input.

    Read lesson 0.2.4

The Hook

GATE rarely asks you to invent a strange DFA. It asks: “how many states does the minimal DFA for this kind of language have?” — starts with, ends with, contains, length conditions, counts mod n, divisibility. Each kind has a standard shape and a state-count formula. Learn the shapes once and these questions take under a minute.

Predict First

Over {a,b}: how many states does the minimal DFA need for “strings that contain aba”? And for “strings that start with aba”?

Core Concept

Prediction check

Contains aba: 4 (progress 0,1,2,3 matched letters; the last state accepts forever — no dead state needed). Starts with aba: 5 (progress 0..3 plus a dead state for a wrong start). The dead state is the difference.

contains aba
q0 q1 q2 q3 — q3 accepts forever, no dead state
starts with aba
q0 q1 q2 q3 + D — a wrong start is fatal
The only difference is the dead state: k + 1 vs k + 2.

Pattern 1 — starts with w (|w| = k)

States: progress 0, 1, …, k (k is accepting and loops on everything) plus a dead state for any mismatch. k + 2 states.

Starts with aba (k + 2 = 5 states)

States: q0, q1, q2, q3, D

Transitions: q0>q1: a; q0>D: b; q1>q2: b; q1>D: a; q2>q3: a; q2>D: b; q3>q3: a,b; D>D: a,b

Starts with w: a straight line of progress, a sink that accepts, and one dead state catching every mismatch.

Pattern 2 — contains w as a substring

States: “longest prefix of w matched so far” 0, …, k. On a mismatch you don’t restart from 0 blindly: you move to the longest prefix of w that is a suffix of what you just read (contains aba: in state 2 = “ab”, reading b gives “abb”, whose longest suffix that is a prefix of aba is ε → state 0; reading a goes to 3). State k accepts and loops. k + 1 states. No dead state.

state (matched so far)on aon b
q0 (ε)q1q0
q1 (a)q1 (aa → keep “a”)q2
q2 (ab)q3 ✓q0 (abb → nothing)
q3 (aba found)q3q3
Failure moves: fall back to the longest suffix that is still a prefix of aba.

Pattern 3 — ends with w

Same “longest prefix of w that is a suffix of the input” states 0, …, k, but state k does not loop — further symbols can leave it. k + 1 states. (Ends with 01: 3 states, as in 1.1.2.)

Ends with aba (k + 1 = 4 states)

States: q0, q1, q2, q3

Transitions: q0>q0: b; q0>q1: a; q1>q1: a; q1>q2: b; q2>q3: a; q2>q0: b; q3>q1: a; q3>q2: b

Ends with w: same failure moves, but the accepting state is not a sink. Try abab: it leaves q3.

Pattern 4 — length conditions

  • |w| ≡ r (mod n): a cycle of n states, one per remainder.
  • |w| = k exactly: k + 2 (counter 0..k, plus “too long” dead).
  • |w| ≤ k: k + 2 (0..k accepting, then dead).
  • |w| ≥ k: k + 1 (0..k, last one accepting with self-loop).
|w| ≡ 0 (mod 3): a cycle of 3

States: r0, r1, r2

Transitions: r0>r1: a,b; r1>r2: a,b; r2>r0: a,b

Length mod n: n states in a cycle, one per remainder.

Pattern 5 — counting symbols mod m and n

“#a ≡ i (mod m) and #b ≡ j (mod n)”: a grid of states (#a mod m, #b mod n) → m · n states. The same grid works for “or”, “not”, any combination — only F changes. Minimal for most conditions (a condition that ignores one coordinate collapses it).

#b ≡ 0#b ≡ 1#b ≡ 2
#a evene0 ✓e1e2
#a oddo0o1o2
The 2 × 3 grid: a moves you between rows, b moves you along columns. Change which cells are accepting to get “and”, “or”, “not” versions — still 6 states.

Pattern 6 — divisibility of numbers

Read a binary number left to right. If the value so far is v, reading bit b gives 2v + b. Track only r = v mod k: new r = (2r + b) mod k. So k states, start and accept at r = 0. For decimal: new r = (10r + d) mod k.

k states always suffice; the minimal number can be smaller when k shares factors with the base. Binary, divisible by 2: 2 states; by 4: 3 states (it is just “ends with 00”); by 8: 4 states; by 6: 4 states. For odd k in binary, all k remainder states are needed.

read1101
value so far13613
remainder mod 31001
Divisibility: only the remainder matters. r ← (2r + bit) mod 3. 1101 = 13 → remainder 1 → reject.
1112223334345556467778489991061011111112512
k states (remainders)minimal DFA (m + j)
Binary ÷ k: k states always work, but for even k the minimal DFA is smaller (k = 2ʲ·m, m odd ⇒ m + j). Verified by computer for k ≤ 24.

Pattern 7 — the k-th symbol from the start

“3rd symbol is 1”: skip 2 symbols (states 0,1,2), then 1 → accept-forever, 0 → dead. k + 2 states. (The k-th symbol from the end is a very different story — Chapter 1.2.)

3rd symbol from the start is 1 (k + 2 = 5 states)

States: q0, q1, q2, A, D

Transitions: q0>q1: 0,1; q1>q2: 0,1; q2>A: 1; q2>D: 0; A>A: 0,1; D>D: 0,1

k-th symbol from the start: skip k − 1 symbols, then branch to accept-forever or dead.

Putting it together

  • Starts-with: k + 2. Contains / ends-with: k + 1, with fall-back moves.
  • Length mod n: n states. Two counters mod m and n: an m × n grid.
  • Binary ÷ k: r ← (2r + b) mod k; minimal m + j for k = 2ʲ·m.

Visual

Contains aba as a substring

States: q0, q1, q2, q3

Transitions: q0>q0: b; q0>q1: a; q1>q1: a; q1>q2: b; q2>q3: a; q2>q0: b; q3>q3: a,b

contains aba — failure moves explained
state = longest prefix of "aba" that is a suffix of what was read
 q0 ""   a→q1 "a"        b→q0
 q1 "a"  a→q1 "a" (aa: suffix "a")   b→q2 "ab"
 q2 "ab" a→q3 "aba" ✓    b→q0 (abb: no suffix is a prefix of aba)
 q3      a,b→q3 (already found)
traces: "babab": q0 b q0 a q1 b q2 a q3 b q3  ACCEPT
        "abba" : q0 a q1 b q2 b q0 a q1        REJECT
Binary value divisible by 3 (state = remainder)

States: r0, r1, r2

Transitions: r0>r0: 0; r0>r1: 1; r1>r0: 1; r1>r2: 0; r2>r1: 0; r2>r2: 1

divisible by 3: r' = (2r + b) mod 3        trace 110 (=6): r0 ─1→ r1 ─1→ r0 ─0→ r0  ACCEPT
        0    1                                trace 111 (=7): r0 ─1→ r1 ─1→ r0 ─1→ r1  REJECT
→*r0    r0   r1
  r1    r2   r0
  r2    r1   r2         (ε stays in r0 and is accepted — by convention ε has value 0)
#a even AND #b ≡ 0 (mod 3) — 2 × 3 grid

States: e0, e1, e2, o0, o1, o2

Transitions: e0>o0: a; o0>e0: a; e1>o1: a; o1>e1: a; e2>o2: a; o2>e2: a; e0>e1: b; e1>e2: b; e2>e0: b; o0>o1: b; o1>o2: b; o2>o0: b

Worked Examples

Easy

Minimal DFA states for strings over {a,b} whose length is divisible by 4?

Show the full solution

A 4-cycle of remainders: 4. All four are needed (remainder r needs 4−r more symbols to reach acceptance — different for each r).

Medium

Minimal DFA for strings over {0,1} that end with 00. Give the table and trace one accepted and one rejected string.

Show the full solution

States = longest suffix matching a prefix of 00: A (none), B (“0”), C (“00”, accept). A: 0→B, 1→A. B: 0→C, 1→A. C: 0→C, 1→A. 3 states. Trace 1100: A→A→A→B→C accept. 0010: A→B→C→A→B reject. (Note C on 0 stays C — “000” still ends with 00.)

GATE-level

Minimal DFA states over {a,b} for: (i) #a ≡ 0 (mod 3) and #b ≡ 0 (mod 2); (ii) #a ≡ 0 (mod 3) or #b ≡ 0 (mod 2); (iii) the third symbol from the start is b; (iv) |w| ≥ 3 and w starts with a.

Show the full solution

(i) grid 3·2 = 6. (ii) same grid, different F: still 6 (all cells distinguishable: from each cell, the set of “b-counts and a-counts to add” that lead to acceptance differs). (iii) states 0,1,2 skipping, accept-forever, dead: 5. (iv) start; after “a”: need ≥ 2 more (counter states “a”, “a?”, then accept-forever “a??+”), dead for b first: start, 1, 2, 3(accept loop), dead = 5.

GATE Traps

  • “Starts with” needs a dead state (k+2); “contains” and “ends with” do not (k+1).
  • In “ends with” and “contains”, a mismatch does not always send you back to the start — use the longest suffix that is still a prefix.
  • Binary divisibility by k: k states is an upper bound; for even k the minimal DFA is smaller (÷4 → 3 states).
  • “Exactly k” and “at most k” length conditions both need a dead state.

Memory Hook

Starts-with locks the door behind a wrong first step (dead state). Contains and ends-with just slide back to the best partial match.

Connect the Dots

We now design single DFAs fast. But GATE often combines conditions: “even a’s AND ends with b”, “starts with a OR has odd length”. Next lesson builds DFAs for combinations automatically, by running two DFAs side by side — and flips a DFA to get its complement.

Self-Test

  1. NAT

    Minimal DFA states over {a,b} for strings that start with abb?

    Write your answer before opening the key.

  2. NAT

    Minimal DFA states over {a,b} for strings that end with abb?

    Write your answer before opening the key.

  3. NAT

    Minimal DFA states for binary numbers divisible by 5?

    Write your answer before opening the key.

  4. MSQ

    Which languages over {a,b} have a minimal DFA with exactly 3 states?

    1. |w| ≡ 0 (mod 3)
    2. w ends with ab
    3. |w| = 1
    4. w contains bb
  5. MCQ

    Minimal DFA states for “#a ≡ 1 (mod 4) and #b ≡ 2 (mod 3)” over {a,b}:

    1. 7
    2. 12
    3. 4
    4. 3

Rule Card

Language (|w| = k pattern)Minimal states
starts with wk + 2
contains w / ends with wk + 1
|x| ≡ r mod nn
|x| = k, |x| ≤ kk + 2
|x| ≥ kk + 1
#a mod m and/or #b mod nm·n (typical)
binary value ≡ 0 mod k, k oddk
binary ÷ 2ʲj + 1
k-th symbol from start fixedk + 2

Divisibility update: r ← (base·r + digit) mod k.

Concept Ledger

  • DFA design patterns
  • failure transition (longest suffix that is a prefix)
  • remainder states (mod n)
  • grid / counter DFA (m·n states)
  • divisibility DFA r ← (2r+b) mod k

Answer Vault

Reveal answers when you’re ready

Warm-up answers

  1. When q₀ ∈ F.
  2. 64.
  3. Yes — 5 remainders, 11 numbers.

Self-test answers

  1. 5 — k + 2 = 3 + 2.
  2. 4 — k + 1.
  3. 5 — 5 is odd, so all 5 remainders are distinguishable.
  4. ABCD — A: 3 remainders. B: k+1 = 3. C: k+2 = 3. D: k+1 = 3.
  5. B — Grid 4 × 3.
    • A: Adding instead of multiplying.
    • C: Ignores b.
    • D: Ignores a.

Flashcards

  1. Minimal states: strings starting with a fixed w, |w| = k?
    Reveal answerk + 2
  2. Minimal states: strings containing w (|w| = k)?
    Reveal answerk + 1
  3. Minimal states: binary numbers divisible by 4?
    Reveal answer3 (it is “ends with 00”)
  4. Divisibility-by-k DFA transition for binary?
    Reveal answerr ← (2r + b) mod k
  5. #a mod m and #b mod n — states?
    Reveal answerm · n

Exam shortcuts

  • Binary divisible by k = 2ʲ·m (m odd): the minimal DFA has m + j states. ÷6 → 3 + 1 = 4, ÷12 → 3 + 2 = 5, ÷8 → 1 + 3 = 4.
  • For “contains w” the accept state is a sink; for “ends with w” it is not — that single difference is everything.

Ready for the next idea?

Mark this item complete when you’ve finished reading and working through it.