Compute OSPF Shortest Path First Next Hops With ECMP

Last updated: November 29, 2026

Quick Overview

Given a router topology with link costs and a root router, run shortest path first and return, for every destination router, the total cost and the full set of equal-cost first-hop neighbors that should be installed in the forwarding table.

Cisco
Coding & Algorithms
Software Engineer
Cisco
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Given a router topology with link costs and a root router, run shortest path first and return, for every destination router, the total cost and the full set of equal-cost first-hop neighbors that should be installed in the forwarding table.

OSPF and IS-IS SPF runs on every IOS XE and IOS XR router, and Cisco control plane teams care that a candidate can extend Dijkstra beyond distances to the next hops that actually get programmed into hardware. The interviewer checks correct ECMP tie handling and next-hop inheritance.

What the Interviewer Expects
  • Uses Dijkstra with a min-heap
  • Tracks next hops, not just predecessors
  • Sets next hop to the neighbor itself for nodes adjacent to the root
  • Unions next hops on equal-cost ties
  • Explains why positive costs matter for correctness
Key Topics to Cover
Dijkstra
OSPF
ECMP
graphs
routing
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 incremental SPF avoid a full recompute when one link cost changes?
  • What are loop-free alternates and how would you compute one?
  • How do you cap ECMP at 8 or 16 paths like real hardware, and which paths do you keep?
  • What changes with broadcast segments and designated routers?
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

Links are bidirectional (u, v, cost) with positive integer costs. Output: {dest: (cost, [next hops])} for all reachable routers. A next hop is a neighbor of the root. If two shortest paths leave t...

High-Level Design

Standard Dijkstra, but each node also carries a set of first hops. When relaxing edge u -> v: the candidate first hops are {v} if u is the root, else hops[u]. A strictly shorter path replaces ...


Submit Your Answer
Markdown supported

Related Questions