Automata and Languages
Finite and pushdown models, grammars and the parsers built on them, plus how source text becomes runnable code. Interviewers probe it to see if you know why a pattern cannot match nesting.
part ofComputer science fundamentalsoverview, primer and where to startread it →on this pageshowhide
explore
- Finite-State Recognizers25 questions
- Deterministic Acceptors5 questions
- Nondeterminism & Subset Construction5 questions
- Minimization & Equivalence5 questions
- Closure & Decision Problems5 questions
- Pumping Lemma Proofs5 questions
- Regex Formalism20 questions
- Kleene Operators5 questions
- Match Semantics5 questions
- Engine Architectures5 questions
- Exponential Backtracking5 questions
- Context-Free Grammars21 questions
- Derivations & Parse Trees5 questions
- Ambiguity & Precedence5 questions
- Left Recursion & Factoring5 questions
- Pushdown Stack Machines6 questions
- Machine Power Tiers14 questions
- Chomsky Hierarchy Levels5 questions
- Turing Tape Model5 questions
- Turing Completeness4 questions
- Syntax Analysis26 questions
- Recursive Descent Parsers5 questions
- Shift-Reduce & LR Tables6 questions
- Precedence Climbing5 questions
- Ordered Choice & Packrat5 questions
- Error Recovery & Diagnostics5 questions
- Translation Pipeline28 questions
- Tokenization & Maximal Munch6 questions
- AST & IR Forms6 questions
- Symbol Tables & Scoping5 questions
- Optimization & Lowering6 questions
- Interpretation and JIT Tiers5 questions
- Computer Scienceskillanchors this topic
- AI & Data Scientistrole
- Backend Developerrole
- Blockchain Developerrole
- Data Analystrole
- Data Engineerrole
- Forward Deployed Engineerrole
- Full Stack Developerrole
- Game Developerrole
- Java Backend Developerrole
- Kotlin Backend Developerrole
- Machine Learning Engineerrole
- Server-Side Game Developerrole
- Software Architectrole
questions
134 · 6 sectionsA deterministic finite acceptor reads a digit string one symbol at a time — what decides that it accepts?
basics
~20 sOne fact decides it: the state the machine is left in after the final symbol. If that state is in the accepting set, the string is accepted. Passing through an accepting state earlier in the run means nothing.
You have two finite automata over the same alphabet; how do you build one recogniser that accepts exactly the strings both accept?
basics
~20 sRun both machines at once. The product automaton's states are pairs (p, q), one component per machine, and each input symbol advances both components. Mark a pair accepting when both components accept, and you have the intersection.
Why must a deterministic finite acceptor define a transition for every state and symbol, including invalid ones?
basics
~20 sDeterminism means exactly one successor for every state-and-symbol pair, so the transition rule must be total and no run can ever get stuck. Symbols a rule forbids are usually routed to a non-accepting trap state that absorbs the rest of the input.
In a deterministic finite acceptor for badge sequences, what makes two of its states distinguishable rather than mergeable?
basics
~20 sTwo states are distinguishable when some remaining input is accepted from one and rejected from the other; that string is the witness. States with no such witness behave identically on every future input and can be merged without changing the accepted language.
In a nondeterministic finite automaton, a symbol can lead to several states or none — what does it mean to accept a string?
basics
~20 sAcceptance is existential: the machine accepts a string when at least one run over it ends in an accepting state. Runs that die on a missing transition, or finish in a non-accepting state, simply contribute nothing.
In a regular expression, what is the difference between a greedy quantifier and a lazy one?
basics
~20 sA greedy quantifier takes as many repetitions as it can and gives characters back only when the rest of the pattern fails; a lazy one takes as few as possible and grows one at a time.
In a route-matching pattern, what does the Kleene star applied to a subexpression mean?
basics
~20 sThe Kleene star means zero or more repetitions of the subexpression it follows, joined end to end. Zero is the trap: a starred part can match the empty string, so on its own it never forces that text to be present.
Why can a regex engine that advances a set of states in one pass not support backreferences?
basics
~20 sA live state set records where the pattern is, not what the text matched. A backreference demands that an arbitrarily long captured substring occur again, and no fixed set of machine states can carry that text forward.
A regex engine can run a pattern by backtracking or by advancing a set of states - how does each execute a match?
basics
~10 sA backtracking engine explores one alternative at a time and rewinds the input when a branch fails. A state-set engine advances every live alternative together, consuming each input character exactly once.
Why does an unanchored regular expression accept input that its author meant to reject?
basics
~20 sSearching asks whether a match exists anywhere in the subject, not whether the whole subject matches. Without a start and end anchor, a pattern for four digits happily matches the four digits buried inside a longer string.
In a grammar written down as production rules, what distinguishes a terminal from a nonterminal?
basics
~10 sTerminals are the literal symbols that appear in the generated text; nonterminals are named placeholders that some rule rewrites. A derivation starts at one designated start symbol and rewrites nonterminals until only terminals remain.
Under a condition grammar, one saved rule string has two parse trees — what does calling that grammar ambiguous mean?
basics
~20 sAmbiguity means the grammar admits two distinct parse trees for one string, so the tree — and therefore the meaning the string denotes — is not fixed by the grammar alone. Two conforming parsers may legitimately disagree.
How does a leftmost derivation of a string differ from a rightmost derivation of the same string?
basics
~20 sBoth rewrite the same nonterminal occurrences by the same rules, but in a different order: a leftmost derivation always rewrites the leftmost nonterminal of the current line, a rightmost derivation always the rightmost one. The intermediate lines differ; the rule applications do not.
A pushdown automaton is a finite-state control plus one unbounded stack; what does that stack let it recognise that finite state alone cannot?
basics
~20 sThe stack is memory that grows with the input, so the machine can match nesting of any depth: push a marker on every opener, pop and compare on every closer, and accept a run whose store ends exhausted.
How do you rewrite a directly left-recursive rule such as `list -> list ',' item` so a top-down parser can use it?
basics
~20 sSplit the alternatives: replace A -> A alpha | beta with A -> beta A2 and A2 -> alpha A2 | empty. The recursive part becomes a repeating tail, so the parser consumes the leading beta before re-entering and the recursion terminates.
How do you read a grammar's production rules to place it at Chomsky type 3, 2, 1 or 0?
basics
~20 sEach Chomsky tier restricts rule shape. Type 3: one terminal plus at most one non-terminal on the right, every rule leaning the same way. Type 2: a single non-terminal on the left. Type 1: no rule shortens. Type 0: unrestricted.
What does calling an evaluation model Turing complete require you to demonstrate about it?
basics
~20 sTuring complete means the model can simulate an arbitrary Turing machine, so it computes exactly the functions a Turing machine computes. You demonstrate it by building an interpreter for an already-complete model, such as a register machine, inside the candidate.
In a Turing machine specified as a transition table, what exactly does one computation step do?
basics
~20 sA Turing machine step reads the one symbol under the head and, using the table entry for that state-symbol pair, writes a symbol into that same cell, moves the head one cell left or right, and enters the next state.
Which Chomsky tier does a config format with arbitrarily nested blocks require of its validator, and why?
basics
~20 sType 2, context-free. Arbitrarily deep nesting means the number of unclosed blocks is unbounded, and a finite-state validator can distinguish only finitely many histories. The validator needs memory that grows with depth, so it is a parser, not a pattern.
Each Chomsky tier is matched to a recogniser — what memory does each machine add over the tier below?
basics
~20 sOne rung, one kind of memory. A finite-state machine holds only its current state. A pushdown machine adds an unbounded store where only the newest symbol is readable. A linear-bounded machine adds re-readable space no larger than the input. A tape machine removes the size limit.
When a parser recovers from an unexpected token in panic mode, how does it choose where to resume?
basics
~20 sPanic mode discards tokens until it reaches one from a synchronising set — a statement delimiter, block closer or declaration keyword — then unwinds its suspended rules until one accepts that token, and parses on.
When a generated bottom-up parser reads a schema file, what do its shift and reduce actions each do to the parse stack?
basics
~20 sShift pushes the next input token onto the parse stack; reduce recognises a handle, a complete right-hand side sitting on top, pops those symbols and pushes the rule's left-hand nonterminal. The generated table decides which, per state and lookahead.
Why can a parsing expression grammar's ordered choice never produce two different parse trees for one input?
basics
~20 sOrdered choice commits to the first alternative that succeeds at a position, so each rule yields at most one result there. The grammar defines one recognizer rather than a set of permitted derivations, so a second parse tree cannot exist.
In a precedence-climbing parser, what makes an infix operator left-associative rather than right-associative?
basics
~20 sThe power handed to the right-operand recursion decides it: recursing with the operator's power plus one makes it left-associative, because an equally binding operator is then refused; recursing with the same power makes it right-associative.
In precedence climbing, how does one minimum-binding-power loop parse an expression that a layered grammar would need a rule per level for?
basics
~20 sA binding power is a number per operator saying how tightly it grips its neighbours. One loop parses an operand, then keeps absorbing operators whose power clears the caller's minimum, recursing once per right operand.
In a compiler front end, what is a token, and what does the scanner throw away to produce one?
basics
~20 sA token is one lexical unit: a class such as identifier, number, keyword, operator or punctuator, together with its text and the source span it came from. The scanner throws away whitespace and comments but keeps the spans.
Why does a tiered runtime start a method in an interpreter and compile it only after invocation and loop counters cross a threshold?
basics
~20 sCompilation costs time, CPU and memory, and most methods run only a handful of times. Counting invocations and loop iterations finds the small hot fraction where optimisation will be repaid, and the interpreted phase supplies the profile the compiler then optimises against.
What does a compiler drop when it converts a parse tree into an abstract syntax tree?
basics
~20 sAn abstract syntax tree keeps operators, operands and structure, and drops what only guided the parser: grouping parentheses, separators and terminators, layout, and single-child chain nodes from precedence rules. The tree's shape already encodes the grouping those tokens expressed.
What does flattening expressions into three-address instructions with temporaries expose that an expression tree leaves implicit?
basics
~20 sThree-address code gives every intermediate value a name and every operation its own instruction in a fixed order. Evaluation order and the producer of each value become explicit data rather than facts implied by a tree walk.
When several token rules match at one position, how does a maximal-munch scanner pick the token it emits?
basics
~10 sIt takes the longest prefix starting at that position that any rule accepts, not the first rule that matches. If two rules accept the same longest prefix, the rule declared earlier wins.