Code Room2-SAT forced variables
HardPrep Room Coding #1272

2-SAT forced variables

CodingAlgorithms & data structuresSenior–Staff~45 min

Given a 2-SAT instance over n boolean variables (1..n) and a list of clauses (each [a, b] meaning (a OR b), with positive i = variable i true and -i = false), first decide satisfiability. If UNSATISFIABLE, return -1. Otherwise return the count of FORCED variables: variables that take the SAME value in every satisfying assignment. Constraints: 1 <= n <= 2000; clauses may be empty, repeated, or tautological.

Implement
count_forced_variables(n: int, clauses: list[list[int]]) → int
Examples
in[3,[[1,2],[-1,2],[2,3],[-3,1]]]out1
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 45 min
InputExpectedGot
[3,[[1,2],[-1,2],[2,3],[-3,1]]]1not run yetsample