Reported July 2026
Akuna Capitalunion find

Maximum Label Difference in a Connected Component

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

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

The mistake that sinks a first attempt on this Akuna Capital OA, reported in July 2026, is forgetting that isolated vertices and gaps in labels still count as components. You get a graph with gNodes vertices and an edge list, and you need the biggest max-minus-min label spread across any connected component. It's a union-find or BFS problem in disguise, and the logic is short. The traps are in the edges: self-loops, parallel edges, and gNodes of zero. If you blank on the day, StealthCoder runs invisibly during the live OA and gives you a working solution as a safety net.

The problem

You are given an undirected graph with gNodes vertices labeled from 1 through gNodes. For every index i, gFrom[i] and gTo[i] are the endpoints of one undirected edge.
For each connected component, find the difference between its largest and smallest vertex labels. Return the maximum such difference across all connected components. An isolated vertex is a connected component whose difference is 0.

Function
maximumDifference(gNodes: int, gFrom: List<Integer>, gTo: List<Integer>) → int

Examples
Example 1
gNodes = 7
gFrom = [1,2,3,5,6]
gTo = [2,4,5,7,7]
return = 4
The components are {1, 2, 4} and {3, 5, 6, 7}. Their label differences are 4 - 1 = 3 and 7 - 3 = 4, so the answer is 4.
Example 2
gNodes = 4
gFrom = []
gTo = []
return = 0
All four vertices are isolated, so every component has equal minimum and maximum labels and contributes a difference of 0.
Example 3
gNodes = 9
gFrom = [1,6,2,3,4]
gTo = [6,9,3,4,2]
return = 8
The component {1, 6, 9} has label difference 9 - 1 = 8. The cycle {2, 3, 4} has difference 2, and all remaining vertices are isolated.

Constraints
gNodes is nonnegative.
gFrom.length == gTo.length.
Every endpoint is between 1 and gNodes, inclusive.
Parallel edges and self-loops are allowed.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is to group vertices into components, then track the min and max label per component. With union-find, merge each edge pair, then loop over every vertex from 1 to gNodes, find its root, and update min and max for that root. The answer is the largest max minus min. Keep a root-indexed array for both values, initialized from the vertex itself. The common pitfall is only iterating over vertices that appear in edges, which skips isolated nodes. That's harmless for the answer since they give 0, but it breaks if every vertex is isolated and you return something undefined. Another pitfall is recursion depth in find without path compression, which can blow up on long chains. Use iterative find or path halving. Self-loops and duplicate edges are fine, since union of the same root is a no-op. Total cost is near linear. If the logic slips live, StealthCoder is the hedge that hands you the solution without the proctor seeing anything.

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 Maximum Label Difference in a Connected Component 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 Akuna Capital's OA.

Akuna Capital 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.

Maximum Label Difference in a Connected Component FAQ

What's the trick to the Akuna Capital maximum label difference problem?+

Build connected components, then track the smallest and largest label in each one. Union-find does this cleanly. After merging all edges, scan labels 1 to gNodes, find each root, and update that root's min and max. Return the largest gap between them.

Do I need union-find or is BFS fine?+

Either works. BFS or DFS over an adjacency list finds each component and its min and max in linear time. Union-find is shorter to write and handles duplicate edges and self-loops without special cases. Pick whichever you can code without bugs under pressure.

What edge cases break this problem?+

gNodes of zero, empty edge lists, self-loops, and parallel edges. Isolated vertices must count as components with difference 0, so the answer is 0 when there are no edges. Also watch that labels are 1-indexed, so size your arrays to gNodes plus one.

How hard is this really?+

Easy to medium. The algorithm is a standard component problem with one extra bookkeeping step for min and max. If you know union-find, you can write it in a few minutes. The difficulty comes from off-by-one labels and missing isolated vertices, not from the idea.

How do I prepare in 48 hours for this kind of OA question?+

Write union-find from memory with path compression and union by size. Then practice adding per-component data like min, max, or size. Do two or three graph component problems end to end. That covers this one and its close variants.

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

OA at Akuna Capital?
Invisible during screen share
Get it