Finite state machines
Computer Science · Theory of computation
Question 1 of 10
0A finite state machine has states q0 and q1 and reads the symbols 0 and 1. It starts in q0. Transitions: q0: on 0 go to q0, on 1 go to q1. q1: on 0 go to q0, on 1 go to q1. What is the number n of the state qn the machine is in after reading the input 011011?
Decimals (0.75 or 0,75), fractions (3/4) and mixed numbers (1 1/2) are accepted. Do not use thousands separators.
How to type your answer
- Fractions: 3/4. Mixed numbers: 1 1/2.
- Decimals with a point or a comma: 0.75 or 0,75.
- Negative numbers start with a minus: -3/4.
- Percentages need the % sign: 75%.
Stuck? Read the explanation
Unlocks at a streak of 5 correct answers in a row.
Embed this practice on your site or class page
Paste this HTML into your page. Students practise right there; it is free and needs no account.
Finite state machines explained
A finite state machine is a model of a system that can be in one of a fixed number of states. It reads input one symbol at a time, and each symbol causes a transition from the current state to a next state. The machine has a start state, where it begins, and one or more accepting states. After the last symbol is read, the input is accepted if the machine is in an accepting state, and rejected otherwise.
Finite state machines are used in vending machines, traffic lights, lexical analysers in compilers and simple text-matching tasks. A deterministic finite automaton (DFA) has exactly one transition for every state and input symbol. Diagrams usually show states as circles, accepting states as double circles, and transitions as labelled arrows. The machine has no memory beyond its current state, so it can only recognise patterns that a fixed set of states can track.
Worked example
Design a machine over the alphabet {0, 1} that accepts binary strings containing an even number of 1s. It needs two states: Even (the start and accepting state) and Odd. Every 0 keeps the machine in its current state, and every 1 switches between Even and Odd.
Trace the input 1101.
- Start in Even. Read 1, move to Odd.
- Read 1, move to Even.
- Read 0, stay in Even.
- Read 1, move to Odd.
The machine ends in Odd, which is not accepting, so 1101 is rejected. It contains three 1s, an odd number, which confirms the result.
What you will practise
New numbers every time you practise. Levels: Easy, Medium, Hard. Each answer is followed by a worked explanation.
Example questions
Easy
A finite state machine has states q0 and q1 and reads the symbols a and b. It starts in q0. Transitions: q0: on a go to q1, on b go to q0. q1: on a go to q0, on b go to q0. What is the number n of the state qn the machine is in after reading the input bbbaab?
Medium
A finite state machine has states q0, q1 and q2 and reads the symbols x and y. It starts in q0. Transitions: q0: on x go to q0, on y go to q1. q1: on x go to q2, on y go to q0. q2: on x go to q1, on y go to q0. The input xxyyyx is read one symbol at a time. How many times does the machine arrive in state q0 while reading it? Do not count the starting position.
Hard
A Mealy machine has states q0, q1 and q2 and reads the symbols 0 and 1. It starts in q0 and every transition produces an output bit. Transitions: q0: on 0 go to q1 and output 1, on 1 go to q2 and output 0. q1: on 0 go to q0 and output 1, on 1 go to q0 and output 1. q2: on 0 go to q1 and output 0, on 1 go to q2 and output 1. The input 1001001 is read. How many 1s are in the output?