Evaluate Boolean Expression - Problem
Evaluate Boolean Expression
You are given two tables:
The
The
Goal: For each expression, substitute the variable names with their values from the Variables table, then evaluate the comparison. Return
In the example above:
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:| name | value |
|---|---|
| x | 66 |
| y | 77 |
The
Expressions table contains boolean expressions to evaluate:| left_operand | operator | right_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
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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code