Magicsheet logo

Evaluate Boolean Expression

Medium
100%
Updated 6/1/2025

Asked by 1 Company

Topics

Evaluate Boolean Expression

What is this problem about?

The Evaluate Boolean Expression coding problem is a SQL task where you are given three tables: Variables (storing variable names and their values), Expressions (storing comparisons like x > y), and you need to determine if each expression is true or false. You need to join the variables with the expressions and output the result as 'true' or 'false' strings.

Why is this asked in interviews?

This is a common Database interview pattern used to test a candidate's proficiency with multiple joins and the CASE statement. It requires mapping strings to values and performing conditional logic within a query. It's a practical test of how you transform raw data into logical results.

Algorithmic pattern used

The solution involves Double Joins and a CASE Statement.

  1. Join the Expressions table with the Variables table twice: once for the left_operand and once for the right_operand.
  2. Use a CASE statement to evaluate the operator column (>, <, =).
  3. For each row, check the logic: IF left_val > right_val THEN 'true' ELSE 'false'.
  4. Return the expression and the result string.

Example explanation

Variables: x = 10, y = 5. Expressions: x > y, x < y.

  1. For x > y:
    • Join finds value of x (10) and y (5).
    • 10 > 5 is true. Output: x, >, y, true.
  2. For x < y:
    • Join finds 10 and 5.
    • 10 < 5 is false. Output: x, <, y, false.

Common mistakes candidates make

  • Single Join: Trying to join variables only once, which doesn't work when an expression has two different variables.
  • String literals: Returning the boolean values 1 or 0 instead of the required strings 'true' and 'false'.
  • Handling NULLs: Not considering what happens if a variable in an expression isn't present in the Variables table (though the schema usually prevents this).

Interview preparation tip

In SQL, whenever you have a table that references another table multiple times (like a "sender" and "receiver" or "left" and "right" operand), you must perform multiple joins to the reference table using distinct aliases.

Similar Questions