Reported May 2025
Wells Fargograph

Canonical Euler Trail

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

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

Wells Fargo reportedly served this Canonical Euler Trail question in May 2025, and the input size is the first thing that matters. With up to 2 * 10^5 edges and 10^5 vertices, anything that tries every path is dead on arrival. This is Hierholzer's algorithm with a strict tie-breaking rule, so the output has to match exactly, not just be a valid trail. If you've seen Reconstruct Itinerary, you're halfway there. The OA is theater. Now you have the script, and StealthCoder sits invisibly on your screen as a safety net if the iterative DFS or the ordering details slip away mid-assessment.

The problem

You are given a connected undirected multigraph with vertices numbered from 1 through n. Edge i joins edgeFrom[i] and edgeTo[i]. Parallel edges are allowed.
The graph is guaranteed to have an Euler trail: a vertex sequence that uses every edge exactly once. Return the trail produced by this canonical Hierholzer order:
If exactly two vertices have odd degree, start at the smaller one. Otherwise start at the smallest vertex incident to an edge.
Whenever the traversal is at a vertex, consume the unused incident edge whose other endpoint is smallest. If several such edges have the same endpoint, consume the one with the smallest input index.
Use Hierholzer backtracking and reverse the completed postorder sequence.

Function
canonicalEulerTrail(n: int, edgeFrom: int[], edgeTo: int[]) → int[]

Examples
Example 1
n = 3
edgeFrom = [1,2]
edgeTo = [2,3]
return = [1,2,3]
The only Euler trail starts at odd-degree vertex 1 and follows both edges.
Example 2
n = 3
edgeFrom = [1,2,3]
edgeTo = [2,3,1]
return = [1,2,3,1]
All degrees are even, so traversal starts at vertex 1 and takes neighbor 2 first.
Example 3
n = 4
edgeFrom = [1,1,2,3]
edgeTo = [2,3,4,4]
return = [1,2,4,3,1]
Vertices 1 and 4 are odd; start at 1 and consume the edge to 2 before the edge to 3.

Constraints
2 <= n <= 10^5.
1 <= edgeFrom.length == edgeTo.length <= 2 * 10^5.
Every endpoint is between 1 and n, and no edge is a self-loop.
The graph is connected after ignoring isolated vertices and has either zero or two odd-degree vertices.
Every vertex from 1 through n is incident to at least one edge.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is Hierholzer with sorted adjacency. For each vertex, store (neighbor, edge index) pairs sorted ascending, and keep a pointer per vertex. Mark edges used by index so the parallel and reverse copies of an edge aren't taken twice. Start at the smaller odd vertex if there are two odd ones, else at vertex 1, since every vertex has an edge. Run DFS, take the next unused edge, recurse, and push the vertex to the postorder on return. Reverse at the end. The big pitfall is recursion depth: 2 * 10^5 edges will overflow the stack in many languages, so use an explicit stack. Second pitfall is forgetting to skip used edges when advancing the pointer. Sorting costs O(E log E), traversal is O(E). If you blank on the iterative version during the live OA, StealthCoder is the hedge that gets you a working one.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Canonical Euler Trail 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

Wells Fargo reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Canonical Euler Trail FAQ

What's the trick in Canonical Euler Trail?+

Hierholzer's algorithm with adjacency lists sorted by (neighbor, edge index). Walk greedily using the smallest unused edge, push vertices to a list when stuck, then reverse it. The sorted order is what makes the output canonical rather than just any valid Euler trail.

How do I handle parallel edges?+

Give each edge its own index and store it in both endpoints' adjacency lists. Sort by neighbor, then index. Keep a used[] array keyed by edge index so that consuming an edge from one side blocks it from the other side too.

Will recursion work with 2 * 10^5 edges?+

Probably not. Depth can reach the edge count, which overflows the stack in most languages. Write it iteratively with an explicit stack: peek the top vertex, advance its pointer past used edges, push the neighbor if one exists, otherwise pop into the result.

How do I pick the start vertex?+

Compute degrees from the edge lists. If exactly two vertices have odd degree, start at the smaller one. Otherwise start at vertex 1, because the constraints say every vertex has at least one incident edge, so the smallest incident vertex is 1.

How do I prepare for this in 48 hours?+

Code Hierholzer iteratively once from scratch, then add the sorted adjacency and per-vertex pointer. Test on the three examples, especially the triangle with an even-degree start. Also review Reconstruct Itinerary since the pattern and ordering logic are nearly identical.

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

OA at Wells Fargo?
Invisible during screen share
Get it