Cheapest Shared Network Carrying Two Feeds Into One Region

Last updated: November 29, 2026

Quick Overview

Given a directed weighted network, choose a set of edges of minimum total weight such that both source s1 and source s2 can reach destination dest using only chosen edges. Return that weight or -1.

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

0

0

0 solved


Given a directed weighted network, choose a set of edges of minimum total weight such that both source s1 and source s2 can reach destination dest using only chosen edges. Return that weight or -1.

A Meta onsite problem about combining shortest path trees. It can be framed as two upstream data centers both replicating into one region. Interviewers want the insight that optimal routes merge at one node and then share the rest of the way, which reduces the problem to three Dijkstra runs.

What the Interviewer Expects
  • Argue that an optimal subgraph is two paths that merge at a meeting node and then share one path to dest
  • Run Dijkstra from s1, from s2, and from dest on the reversed graph
  • Minimize d1[m] + d2[m] + dd[m] over all meeting nodes m
  • Handle unreachable combinations
  • Explain why reversing edges gives distances to dest
Key Topics to Cover
Dijkstra
Reverse graph
Meeting point
Steiner-like subgraph
Directed graphs
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
  • Why can the two paths not benefit from merging, splitting and merging again?
  • How would this generalize to three sources?
  • What if edges were undirected?
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
  • n up to 10^5, directed edges (u, v, w) with positive weights.
  • Find the minimum total weight of an edge set in which both sources reach dest....
High-Level Design

Once the two routes share a node m, the cheapest continuation is the single shortest path from m to dest, used by both. Before m, each source takes its own shortest path to m. Any optimal su...


Submit Your Answer
Markdown supported