Check Whether Equal and Not-Equal Constraints on Variables Are Consistent
Last updated: November 29, 2026
Quick Overview
Given constraints written as strings like 'a==b' or 'a!=b' over single-letter variables, return True if some assignment of values to variables satisfies all constraints at once.
Meta
November 29, 20260
0
0 solved
Given constraints written as strings like 'a==b' or 'a!=b' over single-letter variables, return True if some assignment of values to variables satisfies all constraints at once.
A Meta technical screen problem in the style of validating rules in a policy or config language, where equalities and inequalities must not contradict each other. The interviewer checks the two-pass approach (union every equality, then test every inequality) and a clear explanation of why order matters.
What the Interviewer Expects
- Process all equalities before any inequality
- Union variables connected by ==
- Fail if any != pair shares a root
- Treat x!=x as an immediate contradiction
- State linear time in the number of constraints
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Why would processing constraints in input order give wrong answers?
- How would you report a minimal set of contradictory constraints?
- How would you extend this to less-than constraints?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input:
constraints, each a 4-character stringx==yorx!=ywith lowercase variables. - Output:
Trueif satisfiable....
High-Level Design
Equality is an equivalence relation, so all == constraints partition variables into classes that must share a value. Any assignment that gives each class a distinct value satisfies every != across...