Evaluate Boolean Expression - Problem

You are given two tables: Variables and Expressions.

The Variables table stores variable names and their integer values. The Expressions table contains boolean expressions with left operand, operator, and right operand.

Your task is to evaluate each boolean expression by looking up the variable values and applying the given operator ('<', '>', or '=').

Return the result of each boolean expression evaluation in any order.

Table Schema

Variables
Column Name Type Description
name PK varchar Variable name (primary key)
value int Integer value of the variable
Primary Key: name
Expressions
Column Name Type Description
left_operand PK varchar Left variable name in expression
operator PK enum Comparison operator: '<', '>', or '='
right_operand PK varchar Right variable name in expression
Primary Key: (left_operand, operator, right_operand)

Input & Output

Example 1 — Basic Boolean Evaluation
Input Tables:
Variables
name value
x 66
y 77
Expressions
left_operand operator right_operand
x > y
x < y
x = y
y > x
Output:
left_operand operator right_operand value
x > y false
x < y true
x = y false
y > x true
💡 Note:

We evaluate each expression by looking up variable values: x=66, y=77. Then: x>y becomes 66>77=false, x<y becomes 66<77=true, x=y becomes 66=77=false, y>x becomes 77>66=true.

Example 2 — Equal Values
Input Tables:
Variables
name value
a 50
b 50
Expressions
left_operand operator right_operand
a = b
a > b
Output:
left_operand operator right_operand value
a = b true
a > b false
💡 Note:

Both variables have equal values: a=50, b=50. So a=b evaluates to true, while a>b evaluates to false since 50 is not greater than 50.

Constraints

  • 1 ≤ Variables.name.length ≤ 20
  • -1000 ≤ Variables.value ≤ 1000
  • operator is one of '<', '>', '='
  • All operands in expressions exist in Variables table

Visualization

Tap to expand
Evaluate Boolean Expression ProblemInput: Variablesnamevaluex66y77Input: Expressionsleftoprightx>yJOIN &EVALUATEOutput: Boolean Resultsleft_operandoperatorright_operandvaluex>yfalse66 > 77 = false
Understanding the Visualization
1
Input Tables
Variables with values and Expressions to evaluate
2
JOIN Operation
Join Variables twice to get operand values
3
Output
Evaluated boolean results
Key Takeaway
🎯 Key Insight: Use double JOINs when you need to compare values from the same lookup table
Asked in
Amazon 12 Microsoft 8
18.2K Views
Medium Frequency
~12 min Avg. Time
485 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