Evaluate Boolean Expression - Problem
Evaluate Boolean Expression

You are given two tables: Variables and Expressions. Your task is to evaluate boolean expressions by looking up variable values and applying comparison operators.

The Variables table stores variable names and their integer values:
namevalue
x66
y77

The Expressions table contains boolean expressions to evaluate:
left_operandoperatorright_operand
x<y

Goal: For each expression, substitute the variable names with their values from the Variables table, then evaluate the comparison. Return 'true' or 'false' for each expression.

In the example above: x < y becomes 66 < 77 which evaluates to 'true'.

Input & Output

example_1.sql โ€” Basic Comparison
$ Input: Variables: [(x,66), (y,77)] Expressions: [(x,<,y)]
โ€บ Output: [(x,<,y,'true')]
๐Ÿ’ก Note: x = 66 and y = 77, so x < y evaluates to 66 < 77 which is true
example_2.sql โ€” Multiple Operations
$ Input: Variables: [(x,66), (y,77), (z,66)] Expressions: [(x,>,y), (x,=,z), (y,>,z)]
โ€บ Output: [(x,>,y,'false'), (x,=,z,'true'), (y,>,z,'true')]
๐Ÿ’ก Note: x > y: 66 > 77 = false, x = z: 66 = 66 = true, y > z: 77 > 66 = true
example_3.sql โ€” Edge Case Equal Values
$ Input: Variables: [(a,50), (b,50)] Expressions: [(a,<,b), (a,=,b), (a,>,b)]
โ€บ Output: [(a,<,b,'false'), (a,=,b,'true'), (a,>,b,'false')]
๐Ÿ’ก Note: When both variables have equal values: < and > are false, = is true

Constraints

  • 1 โ‰ค Variables.length โ‰ค 100
  • 1 โ‰ค Expressions.length โ‰ค 100
  • -109 โ‰ค Variables.value โ‰ค 109
  • All variable names in Expressions are guaranteed to exist in Variables
  • Variable names contain only lowercase English letters

Visualization

Tap to expand
Boolean Expression Evaluation Process๐Ÿ“‹ Variablesx โ†’ 66y โ†’ 77z โ†’ 88โš–๏ธ Expressionx < yleft | op | rightโœ… Result'true'66 < 77LookupEvaluate๐Ÿ”„ Step-by-Step Process1JOIN with Variables to get left operand value (x โ†’ 66)2JOIN again with Variables to get right operand value (y โ†’ 77)3Apply CASE logic: WHEN '<' AND 66 < 77 THEN 'true'4Return final result: 'true'
Understanding the Visualization
1
Load Variables
Store all variable names and their values in memory
2
Process Expression
Take an expression like 'x < y' and look up both values
3
Substitute Values
Replace variable names with their stored values: 'x < y' becomes '66 < 77'
4
Evaluate Comparison
Apply the operator logic and return boolean result as string
Key Takeaway
๐ŸŽฏ Key Insight: Double JOIN eliminates subqueries by efficiently matching variable names to values in a single query execution
Asked in
Amazon 35 Microsoft 28 Google 22 Meta 18
26.8K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen