GATE / CS ARCHIVE

SIMILAR QUESTIONS / THEORY OF COMPUTATION

Track suffix-prefix matches for a fixed ending word

5 questions solved the same way, newest first. Back to Theory of Computation

How these are solved

Build transitions by retaining the longest suffix of the input that is a prefix of the required ending word, including fallback after a full match. 2015 asks state count for ending 10;

2 marks · MSQ
Report issue

Interpreting a suffix-tracking DFA

Finite Automata

Question 50, GATE 2025: A DFA over {a,b} has states q0,q1,q2,q3, start q0 and sole accepting state q3. Transitions on (a,b) are q0→(q0,q1), q1→(q2,q1), q2→(q0,q3), q3→(q2,q1). Which language does it recognize? A words with an even number of b’s; B words containing bab; C words ending in bab; D words avoiding aba.
Tap to enlarge
Select all that apply
Answer & solution

Official answer: C

Approach: Assign each state the longest suffix of the input read so far that is also a prefix of the pattern bab.
The prefixes are ε,b,ba,bab, corresponding respectively to q0,q1,q2,q3. At q0 an a leaves no nonempty matching suffix, while b starts the match. At q1 another b still leaves suffix b, while a extends it to ba. At q2, b completes bab, but a leaves no matching suffix and returns to q0.
After reaching q3, an a leaves suffix ba and returns to q2; a b leaves suffix b and returns to q1. Therefore acceptance records that the current complete word ends in bab, not merely that bab appeared at some earlier position. This proves C.
B fails because baba contains bab but ends at nonaccepting q2. A fails because the empty word has an even b-count but is rejected. D fails because a avoids aba yet is rejected.

Original paper, page 50 · Answer key

2 marks · MCQ
Report issue

Finite Automata

Question 38, GATE 2021: Which DFA recognizes exactly binary strings ending in 011? Essential transition-table descriptions of the four diagrams: all have states q0,q1,q2,q3, start q0, only q3 accepting. Common transitions: q0 on 0→q1 and on 1→q0; q1 on 0→q1 and on 1→q2; q2 on 0→q1 and on 1→q3. The differing q3 transitions (on 0,on 1) are A (q1,q3); B (q3,q3); C (q1,q2); D (q1,q0).
Tap to enlarge
Your answer
Answer & solution

Official answer: D

Label the four states by the longest suffix matching a prefix of 011: ε, 0, 01, 011. Their transitions on(0, 1) are respectively(0,ε),(0, 01),(0, 011),(0,ε). Only the 011 state accepts. These transitions are exactly D. A wrongly accepts 0111, B keeps accepting after arbitrary suffixes, and C accepts 01111 despite its final three symbols being 111.

The invariant is that the state remembers the longest suffix of the input that is also a prefix of the target 011. After reading 0111 there is no nonempty suffix that is such a prefix, so the accepting state's 1-transition must return to q0. After reading 0110, the suffix 0 remains useful, so its 0-transition must go to q1. These two checks distinguish D from the three other diagrams while the invariant proves correctness for arbitrary input lengths.

Original paper, page 32 · Answer key

2 marks · NAT
Report issue

Finite Automata

Question 45, GATE 2015: How many states does the minimal DFA for(0+1)*(10)have?
Tap to enlarge
Answer & solution

Official answer: 3

Step through it (2 parts)

The language consists of binary strings ending 10. Track no matching suffix, suffix 1, and suffix 10 (accepting). On0 the transitions are q0, q2, q0; on 1 all go to q1. These three states are distinguishable: q2 accepts empty suffix, and suffix 0 distinguishes q1 from q0. Thus 3.

Track the longest suffix that is also a prefix of target 10: empty, 1, and 10. Reading 1 always leaves suffix 1. Reading 0 moves from suffix 1 to accepting 10, and otherwise to empty. These three states suffice. They are also necessary: accepting 10 differs from both nonaccepting states on ε, and suffix 1 differs from empty on continuation 0. This distinguishability proof prevents merely guessing the count from the pattern length.

Original paper, page 41 · Answer key

2 marks · MCQ
Report issue

DFA accepting a two-zero suffix

Finite Automata

Question 41, GATE 2009: A DFA starts q0, accepts only q2. On 0, q0→q1→q2→q2; on 1, all states go q0. What does it accept? A strings beginning 0 or 1; B ending 0; C ending 00; D containing 00.
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

The states record zero, one, or at least two consecutive zeros at the end. Every 1 returns to the start state; 0 advances to the accepting state after the second consecutive zero, where further zeros loop. Hence exactly strings ending 00 are accepted, C, rather than all strings that merely contain 00 somewhere.

The state records the length of the current suffix of consecutive zeros, capped at 2. Reading 1 resets that suffix to length 0. Reading a second consecutive 0 reaches q2; additional zeros stay accepting. A string such as 001 contains 00 but ends in 1 and is rejected, separating C from D. A single 0 reaches q1 and is rejected, separating C from B.

Original paper, page 9

Descriptive
Report issue

Four-state DFA for strings ending in abb

Finite Automata

Question 21(b), GATE 2002: Original21(b): Construct a four-state DFA for the same language (a|b)*abb; specify all transitions and accepting state.
Question 21(b), GATE 2002: Original21(b): Construct a four-state DFA for the same language (a|b)*abb; specify all transitions and accepting state.
Tap to enlarge
Answer & solution

Answer: Start q0, accept q3. On (a,b): q0→(q1,q0); q1→(q1,q2); q2→(q1,q3); q3→(q1,q0). (no official key was published)

Step through it (2 parts)

Use state qj to record the length j of the longest suffix of the input seen so far that is also a prefix of abb. The four represented suffixes are empty, a, ab, abb. Start at q0 and accept only q3. The transition table is:
State | a | b
q0 | q1 | q0
q1 | q1 | q2
q2 | q1 | q3
q3 | q1 | q0
For example, reading a after ab or abb leaves suffix a, while reading b after abb leaves no nonempty suffix that begins abb. This invariant proves that q3 is reached at end exactly for strings ending in abb. All transitions are defined and exactly four states are used.

Original paper, page 10