What are NP, NP-Complete, and NP-Hard problems?
Why Interviewers Ask This
Interviewers ask this to evaluate whether you have the depth of knowledge needed to mentor others and lead technical decisions. The expected answer goes beyond definitions into practical implications and real-world consequences.
Answer
Complexity classes for decision problems: P (Polynomial time): problems solvable in polynomial time O(n^k) by a deterministic Turing machine. Examples: sorting, shortest path, primality (AKS). NP (Nondeterministic Polynomial): problems where a proposed solution can be verified in polynomial time. Examples: Subset Sum, TSP, 3-SAT, Graph Coloring. P ⊆ NP (solving implies verifying). Whether P = NP is the most important open problem in computer science ($1M Millennium Prize). NP-Hard: every NP problem can be reduced to it in polynomial time — as hard as the hardest NP problems. NP-Hard problems may or may not be in NP. Examples: Halting problem (not in NP), TSP optimization version. NP-Complete: both NP and NP-Hard — the hardest problems in NP. If any NP-Complete problem is solvable in polynomial time, then P = NP. Examples: 3-SAT, Vertex Cover, Independent Set, Clique, Hamiltonian Path, Travelling Salesman (decision), Subset Sum, Graph Coloring (3+ colors), Knapsack. Reductions: Cook's theorem proved 3-SAT is NP-Complete; all others proved NP-Complete by polynomial-time reduction from a known NP-Complete problem. Practical handling: approximation algorithms (near-optimal), heuristics, fixed-parameter tractability, or exponential algorithms for small inputs.
Pro Tip
Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a Data Structures & Algorithms codebase.
Previous
What is the Master Theorem for solving recurrences?
Next
What is the Boyer-Moore voting algorithm?