Command Parsing And Predicate Evaluation
Asked of: Software Engineer
Last updated

What's being tested
This tests command parsing, in-memory relational modeling, and predicate evaluation under clean coding constraints. The interviewer is looking for a small database engine: tokenize commands, store rows/columns efficiently, evaluate WHERE-style comparisons, and compose predicates without hard-coding one-off cases.
Patterns & templates
-
Tokenizer + parser split — separate
parse(command)fromexecute(ast); avoid mixing string handling with table mutation logic. -
Row-oriented storage — use
dict[str, list[dict[str, Any]]]for simple queries;O(rows)scans are acceptable unless indexing is requested. -
Schema validation — track column names/types per table; reject unknown columns, wrong arity, and malformed commands before execution.
-
Predicate interface — model filters as
Predicate.evaluate(row) -> bool; implementComparisonPredicate,AndPredicate,OrPredicate, andNotPredicate. -
Operator dispatch table — map strings like
=,!=,<,<=,>,>=to functions fromoperator; centralizes type and comparison behavior. -
Recursive descent parsing — for compound expressions, parse precedence as
OR -> AND -> NOT -> comparison; parentheses require a token cursor. -
Complexity accounting — simple
SELECT ... WHEREisO(n * p)fornrows andppredicate nodes; memory isO(tables + rows).
Common pitfalls
Pitfall: Treating parsing as ad hoc
split(" ")logic breaks quoted strings, parentheses, negative numbers, and compound predicates.
Pitfall: Evaluating predicates while parsing makes the code brittle; build an expression tree first, then execute it against each row.
Pitfall: Forgetting edge cases like missing tables, duplicate columns,
NULL-like values, empty result sets, and string-vs-number comparisons.
Practice these
The practice card below covers the canonical variant — solve it end-to-end, then time yourself while adding compound predicates and validation.
Featured in interview prep guides
Practice questions
Related concepts
- Parsing And Expression EvaluationCoding & Algorithms
- Expression ParsingCoding & Algorithms
- String Parsing, Tokenization, And ValidationCoding & Algorithms
- String Processing, Parsing, And Output FormattingCoding & Algorithms
- String Parsing, Palindromes, And NormalizationCoding & Algorithms
- Coding, Data Structures, And Parsing