Coding

Find the shortest path visiting all nodes in a graph.

You have an undirected, connected graph of n nodes. Return the length of the shortest path that visits every node. You may start and stop at any node, you may revisit nodes multiple times, and you may reuse edges.

Input: graph = [[1],[0,2],[1]]

Output: 2

Explanation: The graph is a straight line: 0-1-2. The shortest path visiting all nodes is 0 -> 1 -> 2, which requires 2 steps.

Was asked at

Practice this question with AI

First session is free - no credit card required.

Go Premium

More interviews, more skills, more success.

Community Answers

1 answer from the community

Unlock Full Access

Answer any un-answered question from our database to unlock all community answers for 30 days.

Give & Take

Practice More Questions

1 answer published. Last candidate submission on Aug 27 2026, 8:29am PDT. Interview question asked to Software Engineers interviewing at MakeMyTrip, Apptio, Noon and other companies. Original question asked: Find the shortest path visiting all nodes in a graph.. Question last reported by a candidate interviewing at Flexport for a Software Engineer interview on Oct 13 2026, 10:58pm PDT.