QA
Senior LevelData Structures & Algorithms

What are P, NP, NP-Complete, and NP-Hard complexity classes?

❓ Interview Prompt: Explain P vs NP, NP-Completeness, polynomial-time reductions, and famous NP-Complete problems.

Complexity Classes:

β€’
P: Problems solvable in polynomial time $O(N^k)$ β€” e.g., Sorting, Shortest Path.
β€’
NP: Problems whose solutions can be verified in polynomial time β€” e.g., TSP, Subset Sum, SAT.
β€’
NP-Complete: NP problems that are as hard as all other NP problems (every NP problem reduces to them in polynomial time).
β€’
NP-Hard: At least as hard as NP-Complete; may not even be in NP (e.g., Halting Problem).

P vs NP (Millennium Prize Problem):

Is $P = NP$? If so, every verifiable problem is efficiently solvable. Most believe $P \neq NP$.

Famous NP-Complete Problems:

SAT, 3-SAT, TSP (decision), Knapsack (decision), Graph Colouring, Clique, Hamiltonian Path.

Code Implementation Example

markdown
Complexity Class Hierarchy:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                  NP-Hard                  β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚           NP-Complete              β”‚  β”‚
β”‚  β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚  β”‚
β”‚  β”‚  β”‚           NP                  β”‚ β”‚  β”‚
β”‚  β”‚  β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚ β”‚  β”‚
β”‚  β”‚  β”‚  β”‚           P             β”‚  β”‚ β”‚  β”‚
β”‚  β”‚  β”‚  β”‚   Sorting, Dijkstra,   β”‚  β”‚ β”‚  β”‚
β”‚  β”‚  β”‚  β”‚   BFS, Matrix Multiply β”‚  β”‚ β”‚  β”‚
β”‚  β”‚  β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚ β”‚  β”‚
β”‚  β”‚  β”‚  TSP (verify), 3-SAT, Knapsackβ”‚ β”‚  β”‚
β”‚  β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚  β”‚
β”‚  β”‚  TSP (decision), SAT, Hamiltonian  β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β”‚  Halting Problem, Chess, Go (unbounded)  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
πŸ’‘P βŠ† NP βŠ† NP-Complete βŠ† NP-Hard complexity class hierarchy.
🎯 Interview Key Takeaway

For NP-Complete problems, use approximation algorithms, heuristics, or bitmask DP for small N. Proving P=NP would revolutionise cryptography and AI.

#DSA#NP-Complete#Complexity Theory#P vs NP#Senior
Back to all Data Structures & Algorithms Questions→

More Data Structures & Algorithms Interview Questions

Beginner LevelData Structures & Algorithms

What is the difference between an Array and a Linked List?

Beginner LevelData Structures & Algorithms

What is Big-O Notation and why is it important?

Beginner LevelData Structures & Algorithms

What are Stack and Queue data structures and where are they used?