Predicate Expression Tree Evaluator
Updated
Problem statement
Evaluate a predicate expression tree against one document. The document is an array of unique [field, value] string pairs. The expression is a nonempty array nodes in topological order, and the last row is the root.
["EQ", field, value]is true when the field exists and its stored string equalsvalueexactly.["GT", field, value]is true when the field exists and its signed 64-bit integer value is greater than the signed 64-bit integervalue.["AND", childIndex1, ...]is true when every referenced child is true.["OR", childIndex1, ...]is true when at least one referenced child is true.
Every logical node has at least one child, every child index is smaller than its node index, and all rows are valid. Return the value of the root predicate.
Function
evaluatePredicate(document: String[][], nodes: String[][]) → booleanExamples
Example 1
document = [["team","search"],["age","31"],["city","Paris"]]nodes = [["EQ","team","search"],["GT","age","25"],["AND","0","1"]]return = trueBoth leaf predicates are true, so their AND parent is true.
Example 2
document = [["tier","free"],["visits","4"]]nodes = [["EQ","tier","pro"],["GT","visits","10"],["OR","0","1"]]return = falseNeither leaf predicate is true, so their OR parent is false.
Example 3
document = [["role","admin"],["score","9"]]nodes = [["EQ","role","admin"],["GT","score","20"],["OR","0","1"],["EQ","missing","x"],["AND","2","3"]]return = falseThe inner OR is true, but a missing field makes the other child false, so the root AND is false.
Constraints
1 <= document.length, nodes.length <= 200000- Document field names are unique.
- Field names and values contain 1 to 40 printable characters and do not contain the character that separates rows in the input.
- Every
GToperand is a valid signed 64-bit integer. - Each logical node has at least one valid child index smaller than its own index.
- The last node is the root.
More MongoDB interview questions
- String Interpolation with a Missing-Value ModePHONE SCREEN · Seen Nov 2026
- Build Packages as Dependencies Are DiscoveredPHONE SCREEN · Seen Oct 2026
- Lazy K-Way Union IteratorPHONE SCREEN · Seen Oct 2026
- Finite Web CrawlerPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Recursive Nested JSON Key SearchPHONE SCREEN · Seen Aug 2026
- JSON Path Lookup with Implicit Array WildcardsONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Word BreakAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Flat Records to a Hierarchical TreeAddepar · PHONE SCREEN
- Normalize Loan Merchants to Root BusinessesAffirm · PHONE SCREENAffirm interview questions
- Flat Comments to a Nested TreeByteDance · PHONE SCREENByteDance interview questions
- Insert an Employee into an Organization HierarchyWaymo · PHONE SCREENWaymo interview questions
- Render a Stable Comment HierarchyBobyard · PHONE SCREEN
- Decrypt a String from Binary Tree ColumnsBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Longest Fibonacci-Like SubsequenceAdobe · PHONE SCREENAdobe interview questions
- Longest Common Contiguous Browser HistoryRoblox · PHONE SCREENRoblox interview questions
- Longest Common URL History RunDatadog · PHONE SCREENDatadog interview questions