Magicsheet logo

Minimum Cost Walk in Weighted Graph

Hard
79.6%
Updated 6/1/2025

Minimum Cost Walk in Weighted Graph

1. What is this problem about?

The Minimum Cost Walk in Weighted Graph is a graph theory problem with a bitwise twist. You are given a weighted undirected graph. For any walk (a path that can revisit nodes and edges), the cost is defined as the bitwise AND of all the weights of the edges in the walk. Given several queries consisting of a start node and an end node, you need to find the minimum cost to walk from start to end. If no walk exists, the cost is -1.

2. Why is this asked in interviews?

This "Hard" problem is asked by companies like Microsoft and DE Shaw to test a candidate's understanding of graph connectivity and bitwise properties. The Minimum Cost Walk in Weighted Graph interview question is tricky because the cost is the bitwise AND. Unlike addition, the bitwise AND of more numbers can only stay the same or decrease. This means the "minimum cost" is achieved by including as many edges as possible within a connected component.

3. Algorithmic pattern used

The core algorithmic pattern is Union Find (Disjoint Set Union) or DFS/BFS to find connected components. For each connected component, we calculate the bitwise AND of all edge weights within that component. Since we can revisit any edge in a walk, the minimum cost for any walk between two nodes in the same component is simply the AND sum of all edges in that component. If two nodes are in different components, no walk exists. This "Union Find, Graph, Bit Manipulation interview pattern" simplifies a seemingly complex pathfinding problem into a connectivity and component-property problem.

4. Example explanation

Graph: Edge (0, 1) weight 7 (binary 111) Edge (1, 2) weight 14 (binary 1110) Edge (3, 4) weight 5 (binary 101)

Queries:

  • Walk from 0 to 2: 0 and 2 are in the same component. The AND sum of all edges in this component is 7&14=(0111)2&(1110)2=(0110)2=67 \& 14 = (0111)_2 \& (1110)_2 = (0110)_2 = 6. Cost = 6.
  • Walk from 0 to 3: 0 and 3 are in different components. Cost = -1.
  • Walk from 3 to 4: Cost = 5.

5. Common mistakes candidates make

A frequent error in the Minimum Cost Walk in Weighted Graph coding problem is trying to use Dijkstra's algorithm. Dijkstra is for additive costs, not bitwise AND. Another mistake is thinking you only need to look at a simple path between the nodes. Because it's a "walk," you can include every edge in the connected component to potentially decrease the AND sum. Candidates also sometimes forget to initialize the AND sum correctly (usually with a value like -1 or a large bitmask of 1s).

6. Interview preparation tip

Whenever a graph problem involves bitwise AND or bitwise OR as a cost, connectivity is usually more important than the shortest path. This "Bit Manipulation and Union Find interview pattern" is a common way to test if you can think critically about how operators behave differently than addition. Practice using Union Find to manage component-level properties, as it is a highly efficient way to handle multiple queries on static graphs.

Similar Questions