Shortest Walk That Visits Every Node in a Small Service Mesh

Last updated: November 29, 2026

Quick Overview

A small service mesh is an undirected connected graph of n services (n up to about 12). Return the length of the shortest walk that visits every service at least once, where the walk can start and end anywhere and may revisit nodes and edges.

Microsoft
Coding & Algorithms
Software Engineer
Microsoft
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


A small service mesh is an undirected connected graph of n services (n up to about 12). Return the length of the shortest walk that visits every service at least once, where the walk can start and end anywhere and may revisit nodes and edges.

Microsoft onsite rounds use this to see if a candidate recognizes that the small n invites a bitmask state space. The interviewer wants BFS over (node, visited set) states with a multi-source start, and a clear state count bound.

What the Interviewer Expects
  • Explain why plain BFS on nodes is insufficient
  • Define the state as (current node, visited mask)
  • Start BFS from all nodes simultaneously
  • Bound the work at O(2^n * n^2) or O(2^n * E)
  • Handle a single node graph
Key Topics to Cover
BFS
bitmask
state space search
shortest paths
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you reconstruct the walk?
  • What if edges have weights (Dijkstra over states)?
  • How would you handle n = 40, and what approximation would you use?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: adjacency list graph for n nodes, connected and undirected.
  • Every edge has length 1.
  • Output: minimum number of edges in a walk that touches every node.
  • Revisiting is allowed, and the ...
High-Level Design

The walk's future depends on where we are and which nodes we have already covered, so the state is (node, mask). With unit edge costs, BFS on these states finds the minimum steps to any state whose ...


Submit Your Answer
Markdown supported