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
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

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
union find
equivalence
constraint satisfaction
two pass
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample Answer
Requirements
  • Input: constraints, each a 4-character string x==y or x!=y with lowercase variables.
  • Output: True if 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...


Submit Your Answer
Markdown supported