What is the travelling salesman problem (TSP)?

Why Interviewers Ask This

This is a differentiating question used for senior and lead roles. Interviewers want to see if you can explain not just what happens, but why — and what the trade-offs are in different approaches.

Answer

The Travelling Salesman Problem (TSP) asks: given n cities with pairwise distances, find the shortest route that visits every city exactly once and returns to the starting city. It's NP-Hard (optimization) and NP-Complete (decision version). Exact algorithms: (1) Brute force: try all (n-1)!/2 permutations. O(n!) — only feasible for n ≤ 12; (2) Held-Karp DP: O(2ⁿ × n²) — bitmask DP where dp[mask][i] = minimum cost to visit all cities in mask ending at city i. Feasible for n ≤ 20-25; (3) Branch and bound: prune search tree; better in practice for n ≤ 50. Approximation algorithms: (1) Greedy nearest neighbor: always go to the nearest unvisited city. O(n²) but solution can be up to O(log n) worse than optimal; (2) Christofides algorithm: guaranteed 3/2-approximation for metric TSP — find MST, find minimum perfect matching on odd-degree vertices, find Eulerian circuit, shortcut repeated vertices. O(n³); (3) Lin-Kernighan heuristic: local search — the best practical heuristic for large instances. Applications: logistics routing, circuit board drilling, DNA sequencing, genome assembly. TSP is the canonical example of a combinatorial optimization problem — studying it reveals techniques applicable to many hard optimization problems.

Pro Tip

Before answering, structure your response: one-line definition → real-world analogy → concrete example from a project. This makes even complex Data Structures & Algorithms answers easy to follow.