Reported August 2026
Googlebacktracking

Count Prefix Paths in a Character Graph

Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Google OA. Under 2s to a working solution.
Founder's read

Google's August 2026 OA reports include a graph problem where labels sit on vertices and you count simple paths spelling each prefix of a target string. The detail that matters is the constraint: at most 10 vertices and a target of at most 10 characters. That's a loud hint that brute force is the intended answer. It's a DFS with a visited set, and the trick is counting every prefix in a single pass. If you blank on the recursion or the bookkeeping, StealthCoder runs invisibly on screen as a safety net during the live OA.

The problem

You are given a simple undirected graph. Its vertices are indexed from 0 to labels.length - 1, and labels[i] is the character stored at vertex i. The array edges contains every undirected edge.
For a non-empty prefix of target, a matching path is an ordered sequence of distinct vertices whose consecutive vertices share an edge and whose labels spell that prefix in order. Two paths are distinct when their ordered vertex sequences differ. Because vertices may not repeat within one path, traversing a cycle back to an already used vertex is not allowed.
Return an array counts of length target.length. For every index k, counts[k] is the number of matching paths that spell target.substring(0, k + 1).

Function
countPrefixPaths(labels: String, edges: int[][], target: String) → long[]

Examples
Example 1
labels = "treetr"
edges = [[0,1],[1,2],[2,3],[4,5]]
target = "trees"
return = [2,2,1,1,0]
Vertices 0 and 4 spell t. The paths [0,1] and [4,5] spell tr. Only [0,1,2] extends to tre, and only [0,1,2,3] extends to tree. No path spells trees.
Example 2
labels = "ababa"
edges = [[0,1],[1,2],[2,3],[3,4],[0,4]]
target = "aba"
return = [3,4,4]
There are three starting a vertices and four directed a-b paths. Each of those four paths can extend to one unvisited a vertex, so the final count is also 4.
Example 3
labels = "aaaa"
edges = [[0,1],[1,2],[2,3],[0,3]]
target = "aaaa"
return = [4,8,8,8]
The graph is a four-cycle. Every vertex starts one path, every undirected edge contributes two ordered length-two paths, and each such direction extends uniquely around the cycle without revisiting a vertex.

Constraints
1 <= labels.length <= 10.
1 <= target.length <= 10.
labels and target contain only lowercase English letters.
Every edge is a pair [u, v] with 0 <= u < v < labels.length.
The edge list contains no duplicates, so the input is a simple undirected graph.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Run a DFS from every vertex whose label equals target[0]. Track a visited bitmask or boolean array. At depth k, the path currently spells target[0..k], so increment counts[k] right there, then try each unvisited neighbor whose label equals target[k+1]. One traversal fills the whole answer array, so you don't rerun per prefix. Paths are ordered, so a path and its reverse count separately when both are valid, which is why example 2 gives 4 and example 3 gives 8. The common pitfall is forgetting to unmark the vertex on backtrack, or deduplicating paths that should count twice. Use long for counts. With 10 vertices the worst case is bounded by the number of simple paths, which is small enough. If the recursion gets tangled mid-assessment, StealthCoder is the hedge that hands you the working backtracking skeleton.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Count Prefix Paths in a Character Graph cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Google's OA.

Google reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Count Prefix Paths in a Character Graph FAQ

How hard is the Google Count Prefix Paths problem really?+

Easier than it looks. The constraints cap both the graph and the target at 10, so plain DFS with backtracking passes. The difficulty is careful counting: ordered paths, no repeated vertices, and filling every prefix in one traversal.

What's the trick to getting all prefix counts at once?+

Increment counts[k] whenever your DFS reaches depth k with a valid path. Every path that spells prefix k also passed through prefixes 0 to k-1, so one search from each valid start vertex records everything. No separate run per prefix.

Do reversed paths count as different paths?+

Yes. Two paths differ when their ordered vertex sequences differ. In example 3, each undirected edge gives two ordered length-two paths, which is why the count is 8. In example 2 the a-b paths are counted in each direction that matches the labels.

Why is backtracking safe here with a graph this small?+

With at most 10 vertices, the number of simple paths is bounded and small. You prune early by only stepping to neighbors matching the next target character, so the search is much smaller than the theoretical worst case.

How do I prepare for this in 48 hours?+

Write a DFS with a visited array and backtracking on a tiny graph, then trace example 2 by hand. Check that you unmark vertices on return, use long for counts, and handle a target character that matches no vertex.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Google.

OA at Google?
Invisible during screen share
Get it