Count Prefix-Matching Simple Paths in a Labeled Graph

Last updated: August 9, 2026

Quick Overview

The question asks the candidate to find the number of simple paths in an undirected labeled graph that match each prefix of a given string.

Google
Coding & Algorithms
Software Engineer
Google
August 9, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

16

0

141 solved


The question asks the candidate to find the number of simple paths in an undirected labeled graph that match each prefix of a given string.

This question is typically asked during the technical screen round for software engineering candidates at Google. The interviewer is evaluating the candidate's problem-solving skills, ability to work with graph data structures, and proficiency in algorithm design. A strong candidate should demonstrate a clear understanding of graph traversal techniques and how to efficiently count paths that match specific criteria.

What the Interviewer Expects
  • Explain the approach to traverse the graph and find paths.
  • Identify the appropriate graph traversal algorithm (e.g., DFS or BFS).
  • Discuss how to handle prefixes and count matching paths.
  • Provide a clear and efficient implementation in code.
  • Analyze the time and space complexity of the solution.
Key Topics to Cover
Graph Traversal
Depth-First Search
Breadth-First Search
Path Counting
Prefix Matching
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 optimize your solution for larger graphs?
  • Can you explain how you would handle cycles in the graph?
  • What would you change if the graph were directed instead of 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
Approach

To solve the problem, we need to traverse the graph and count the number of simple paths that match each prefix of the given string. We can use Depth-First Search (DFS) to explore all possible paths s...

Algorithm
  1. Initialize a list to store the counts of matching paths for each prefix.
  2. For each node in the graph, initiate a DFS traversal.
  3. During the DFS, keep track of the current path and the index of ...

Submit Your Answer
Markdown supported

Related Questions