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
y > x true
x = y false
x < y true
x > y false
💡 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 false
a = b true
💡 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 INPUT Variables Table name value x 66 y 77 Expressions Table left op right x > y x < y x = x Example expressions: x > y --> 66 > 77 x < y --> 66 < 77 ALGORITHM STEPS 1 JOIN Tables Link Expressions with Variables on left operand 2 Second JOIN Link with Variables on right operand 3 Apply Operator Use CASE to compare based on <, >, or = 4 Return Result Output true/false for each expression SELECT CASE WHEN op='<' AND v1<v2 THEN 'true' WHEN op='>' AND v1>v2 THEN 'true' ... END AS value FINAL RESULT Output Table left op right value x > y false x < y true x = x true Evaluation: 66 > 77 false (66 is NOT > 77) 66 < 77 true (66 IS < 77) 66 = 66 true (66 IS = 66) OK - All expressions evaluated Key Insight: Use SQL JOINs to replace variable names with their values, then apply CASE statements to evaluate each operator. Join the Variables table twice - once for left operand, once for right operand - to get both values needed for comparison. TutorialsPoint - Evaluate Boolean Expression | Optimal Solution
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