Count shortest distance in graph

Last updated: February 8, 2026

Quick Overview

Given a weighted, undirected graph represented as an adjacency list, write a function to count the number of distinct shortest paths from a starting node to a target node. The function should return the count of these shortest paths, considering that the graph may contain multiple edges and cycles. Input will consist of the graph structure and the two nodes, while the output should be an integer representing the number of shortest paths.

Capital One
Coding & Algorithms
Machine Learning Engineer
Capital One
February 8, 2026
Machine Learning Engineer
Phone Screen
Coding & Algorithms
Hard

5

12

1,312 solved


Given a weighted, undirected graph represented as an adjacency list, write a function to count the number of distinct shortest paths from a starting node to a target node. The function should return the count of these shortest paths, considering that the graph may contain multiple edges and cycles. Input will consist of the graph structure and the two nodes, while the output should be an integer representing the number of shortest paths.

Capital One uses this problem in the Phone Screen to evaluate your algorithmic thinking. They expect you to discuss multiple approaches, analyze trade-offs between them, and implement the optimal solution with clean, readable code.

What the Interviewer Expects
  • Quickly identify the optimal approach and its theoretical basis
  • Handle complex algorithm design with multiple interacting components
  • Write concise, elegant code under time pressure
  • Prove correctness of your approach and discuss alternative solutions
  • Optimize beyond the obvious: discuss constant factor improvements
  • Address follow-up variations and explain how the solution generalizes
Key Topics to Cover
Dynamic programming and memoization
Hash maps and frequency counting
Graph algorithms and traversal
Edge cases and input validation
Time and space complexity analysis
Binary search and divide and conquer
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
  • Can you solve this in a single pass?
  • How would you parallelize this solution?
  • How would you modify your solution to handle streaming input?
Sharpen Your Skills on Codemia

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

Practice DSA Problems
Sample Answer
Problem Analysis

To solve the problem of counting the number of distinct shortest paths in a weighted, undirected graph, we can apply a breadth-first search (BFS) approach combined with dynamic programming concepts. T...

Approach
  1. Graph Representation: Use an adjacency list to represent the graph, where each entry contains a list of tuples representing adjacent nodes and the weights of the edges.
  2. **Initialize Struct...

Submit Your Answer
Markdown supported

Related Questions