Skip to main content
All Questions

Find the minimum moves to visit all critical nodes in a tree and return to the root.

Unlock detailed company stats for this questionUpgrade

A large distributed system is modeled as a general tree of N servers (nodes 1..N), undirected and acyclic. Some servers contain critical logs that must be collected; these are the set S. You begin at server 1. Goal: start at node 1, visit every node in S at least once, and return to node 1. In one move you travel to an adjacent node. Determine the minimum number of moves. Input: integer N; N-1 edges (u v); the set S. Output: a single integer, the minimum number of moves. Example: N = 8, S = {5, 6}, edges 1-2, 2-5, 2-3, 2-6, 1-4, 4-7, 7-8 -> output 6 (journey 1 -> 2 -> 5 -> 2 -> 6 -> 2 -> 1). Follow-up: you may start from either of 2 places, {1, N}; in 1 move you select either start point and move to an adjacent node.

Expect follow-ups like

  • You are allowed to start from 2 places, {1, N}: in one move you can select either of the start points and move to an adjacent node.

Community answers

No answers contributed by the community yet.

Related courses

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.

Course

System Design Interviews

Learn how to answer the latest system design questions across product, infrastructure, and AI domains. Features in-depth video examples, written breakdowns, and helpful reference patterns. Watch senior engineers and managers answer these questions in mock interview videos.