KatInterview

Resolve Boolean Signal Chains with Negation and Cycle Detection

Trees & GraphsAuto-graded · 19 test cases
MediumAuto-gradedTrees & Graphsgraph traversalcycle detectionhash map

Last updated

Tests iterative functional-graph traversal: propagating True/False through chains of references and NOT, while correctly marking cycles, dangling references, and their dependents as unresolvable.

Problem

You are building a signal propagation system for a hardware simulator. Each signal has a unique string name and is defined by exactly one of the following expressions:

  • "TRUE" — the signal is always on
  • "FALSE" — the signal is always off
  • "<name>" — the signal copies the value of the named signal
  • "NOT <name>" — the signal is the logical inverse of the named signal

Signals may form long dependency chains spanning multiple disconnected groups. A signal cannot be resolved if any of the following apply:

  • It depends on a name that does not appear in the signal table (undefined reference).
  • It is part of a dependency cycle (e.g., P depends on Q and Q depends on P).
  • It depends on another signal that itself cannot be resolved.

Write a function evaluate_signals that accepts the signal table and returns the resolved value of every signal.

Input

  • signal_table: a dictionary mapping each signal name (str) to its expression (str).
    • Every expression is one of: "TRUE", "FALSE", a bare name, or "NOT <name>".

Output

A dictionary mapping every key in signal_table to True, False, or None (unresolvable).

Constraints

  • 1 <= len(signal_table) <= 10^5
  • Signal names consist of alphanumeric characters and underscores only.
  • Your solution must run in O(V + E) time, where V is the number of signals. Because each signal has at most one dependency, E <= V.
  • Do not use recursion; the chain length can reach 10^5.
  • A signal that directly references itself is a cycle and resolves to None.
  • Only keys present in signal_table appear in the output; undefined names are never added.

Examples

Example 1

Input: evaluate_signals({'A': 'NOT B', 'B': 'C', 'C': 'TRUE', 'D': 'NOT C'})

Output: {'A': False, 'B': True, 'C': True, 'D': False}

Example 2

Input: evaluate_signals({'X': 'NOT Y', 'Y': 'X', 'Z': 'Y', 'W': 'TRUE'})

Output: {'X': None, 'Y': None, 'Z': None, 'W': True}

Example 3

Input: evaluate_signals({'A': 'B', 'B': 'NOT MISSING_VAR'})

Output: {'A': None, 'B': None}

Hints

Hint 1

Each signal has at most one outgoing dependency edge, so the graph is a collection of simple chains that may end in a literal, a cycle, or an undefined name — no branching needed.

Hint 2

Walk each chain forward, recording visited nodes in order. Once you find a terminal condition (literal, undefined, cycle, or already-resolved node), sweep back through the recorded path to assign values — flipping the boolean at each NOT node.

Hint 3

Use three states per node — unseen, currently on the active path, and finished — to tell the difference between a real cycle (you reach a node still on the current path) and a node whose value is already known from a previous walk.