KatInterview

Logical Inference Engine: TRUE, FALSE, or Undetermined

Trees & GraphsModel answer
HardModel answerTrees & Graphsgraphimplication2-SAT

Last updated

Build a function that classifies propositions as definitely true, definitely false, or undetermined given a set of facts and implication rules.

Background

Many automated systems — from diagnostic tools to configuration validators — need to reason about what must be true, what must be false, and what simply cannot be settled from the available evidence.

You are given a knowledge base made up of two components:

  1. Facts: propositions that are unconditionally known. A fact written as "X" means variable X is true. A fact written as "!X" means variable X is false.
  2. Rules: one-directional implications of the form "P -> Q". This means: whenever P holds, Q must also hold. Rules carry their contrapositive for free — if Q is false, then P must also be false.

Both P and Q in a rule can be either a plain variable or a negated variable (e.g., "!A -> B" or "A -> !B").

Given these two components, your task is to evaluate a list of query variables and classify each one:

  • "TRUE" — the variable must be true in every consistent interpretation of the knowledge base.
  • "FALSE" — the variable must be false in every consistent interpretation.
  • "CANNOT_DETERMINE" — some consistent interpretations make it true and others make it false.

You may assume the knowledge base contains no contradictions.

Function Signature

def classify_propositions(
    facts: list[str],
    rules: list[str],
    queries: list[str]
) -> list[str]:
    pass

Input and Output

  • facts: strings of the form "X" or "!X".
  • rules: strings of the form "X -> Y" where each side is a plain or negated variable.
  • queries: plain variable names (no ! prefix).
  • Returns a list of "TRUE", "FALSE", or "CANNOT_DETERMINE" in the same order as queries.

Constraints

  • 1 <= len(facts) <= 200
  • 0 <= len(rules) <= 500
  • 1 <= len(queries) <= 200
  • Variable names are alphanumeric, between 1 and 20 characters.
  • The knowledge base is guaranteed to be consistent.

Examples

Example 1

facts = ["SensorActive", "!DoorLocked"]
rules = [
    "SensorActive -> AlarmArmed",
    "AlarmArmed -> MonitoringOn",
    "DoorLocked -> DeadBoltEngaged"
]
queries = ["AlarmArmed", "DeadBoltEngaged", "SensorActive", "DoorLocked"]

# Expected output: ["TRUE", "CANNOT_DETERMINE", "TRUE", "FALSE"]

SensorActive is a fact, so it is TRUE. Following the rule chain, AlarmArmed and MonitoringOn are also TRUE. DoorLocked is directly negated by the facts, so it is FALSE. The rule DoorLocked -> DeadBoltEngaged only fires when DoorLocked is true — since it is false, DeadBoltEngaged is neither forced true nor forced false: CANNOT_DETERMINE.

Example 2

facts = ["!SystemOnline"]
rules = [
    "ServiceRunning -> SystemOnline"
]
queries = ["ServiceRunning"]

# Expected output: ["FALSE"]

The contrapositive of ServiceRunning -> SystemOnline is !SystemOnline -> !ServiceRunning. Since SystemOnline is false, that fires and forces ServiceRunning to be false.

What is expected

Write the full function. Explain your algorithm, handle both forward inference and contrapositive propagation, and discuss complexity and any edge cases worth noting.

Hints

Hint 1

Model each literal (both X and !X) as a node in a directed graph. For every rule P -> Q, add two edges: one for the implication and one for its contrapositive (!Q -> !P). A clean way to encode this is to give variable i the ID 2i for its positive literal and 2i+1 for its negation, so flipping a literal is just XOR with 1.

Hint 2

Start by propagating all known facts through the implication graph via DFS or BFS. Every reachable literal is forced true in every model. This handles the straightforward cases — but it is not sufficient on its own.

Hint 3

Forward reachability misses cases where a variable is forced by contradiction rather than by a direct chain from the facts. To catch these, test each query by temporarily assuming its negation and propagating. If that assumption leads to a conflict (a literal and its negation both become true), then the assumption is impossible and the original variable must be TRUE. Apply the same test in the oth…