Code Room2-SAT satisfiability
HardPrep Room Coding #1025

2-SAT satisfiability

CodingAlgorithms & data structuresSenior–Staff~35 min

You are given n boolean variables (numbered 1..n) and a list of OR-clauses, each with exactly two literals. A literal is encoded as a positive integer v for variable v being true, or -v for variable v being false (e.g. the clause [1,-2] means (x1 OR NOT x2)). Return True if there exists an assignment of the variables that satisfies every clause, else False. Assume 1 <= n <= 2000 and up to a few thousand clauses; a literal may repeat a variable (e.g. [1,1] forces x1 true).

Implement
two_sat_satisfiable(n: int, clauses: list[list[int]]) → bool
Examples
in[2,[[1,2],[-1,2],[-2,1]]]outtrue
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 35 min
InputExpectedGot
[2,[[1,2],[-1,2],[-2,1]]]truenot run yetsample