Conditional Check: Evaluate Boolean Expressions with Variables
Interview Experience
Problem
You are given a list of variable assignments and a list of boolean expressions to evaluate. Each expression is a string of the form "a == b" or "a != b" where a and b are lowercase letters or integer literals.
Return a list of booleans.
Variables may reference other variables (no cycles guaranteed). Evaluate all expressions after resolving the full variable chain.
python
def evaluate_conditions(
assignments: list[tuple[str, str]], # [(var, value_or_var), ...]
expressions: list[str]
) -> list[bool]:
pass
**Input**:
assignments = [("a", "1"), ("b", "a"), ("c", "2")]
expressions = ["a == b", "b != c", "a == 1"]
**Output**: [True, True, True]
**Input**:
assignments = [("x", "y"), ("y", "3")]
expressions = ["x == 3", "x != y"]
**Output**: [True, False]
Follow-ups
- How would you detect and handle circular references in variable assignments?
- Can Union-Find be applied here for equality expressions? How?
- What if expressions include
<and>comparisons on numeric values? - How do you extend this to support compound expressions like
"a == b AND b != c"?
Full Details
Problem
You are given a list of variable assignments and a list of boolean expressions to evaluate. Each expression is a string of the form "a == b" or "a != b" where a and b are lowercase letters or integer literals.
Return a list of booleans.
Variables may reference other variables (no cycles guaranteed). Evaluate all expressions after resolving the full variable chain.
python
def evaluate_conditions(
assignments: list[tuple[str, str]], # [(var, value_or_var), ...]
expressions: list[str]
) -> list[bool]:
pass
**Input**:
assignments = [("a", "1"), ("b", "a"), ("c", "2")]
expressions = ["a == b", "b != c", "a == 1"]
**Output**: [True, True, True]
**Input**:
assignments = [("x", "y"), ("y", "3")]
expressions = ["x == 3", "x != y"]
**Output**: [True, False]
Follow-ups
- How would you detect and handle circular references in variable assignments?
- Can Union-Find be applied here for equality expressions? How?
- What if expressions include
<and>comparisons on numeric values? - How do you extend this to support compound expressions like
"a == b AND b != c"?
About This Question
This is a candidate experience report from a ziphq interview during the phone round.
It covers the following topics: Strings, Phone, Coding, Union Find, Onsite .