Maximum Difference
Reported by candidates from Akuna Capital's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Akuna Capital reported this one in May 2026, and it looks easier than it is. It's a connected components problem in disguise: group nodes, then take max minus min inside each group. The trap is the node that has no edges at all. If your solution only walks the edge lists, isolated nodes never show up, and a few of the examples depend on them. Union-Find or a plain DFS both work fine here. If you blank on the setup mid-assessment, StealthCoder runs invisibly as a backup and can hand you the structure so you're not staring at an empty editor.
The problem
Given a set of nodes and a set of edges between pairs of nodes, first identify the connected components in the graph. A connected component is a group of nodes that are all linked, either directly or through other nodes in the group.
For each connected component, calculate the difference between its largest and smallest node values, and return the maximum of these differences.
Function
maximumDifference(g_nodes: int, g_from: int[], g_to: int[]) → int
Complete the function maximumDifference in the editor below.
maximumDifference has the following parameters:
int g_nodes: the number of nodes in the graph
int g_from[g_edges]: one endpoint of each edge
int g_to[g_edges]: the other endpoint of each edge
Returns: int: the maximum difference between the largest and smallest node value within any connected component
Examples
Example 1
g_nodes = 4
g_from = [1, 2]
g_to = [2, 3]
return = 2
Nodes 1, 2, and 3 form one connected component, whose difference is 3 - 1 = 2. Node 4 is isolated, so its component difference is 0. The maximum difference is 2.
Example 2
g_nodes = 6
g_from = [1, 2, 4]
g_to = [2, 3, 6]
return = 2
The connected components are {1, 2, 3}, {4, 6}, and {5}. Their differences are 2, 2, and 0, so the result is 2.
Example 3
g_nodes = 5
g_from = [1, 4]
g_to = [5, 2]
return = 4
The components are {1, 5}, {2, 4}, and {3}. Their differences are 4, 2, and 0, so the maximum difference is 4.
Constraints
1 ≤ g_nodes ≤ 105
1 ≤ g_edges ≤ min(105, (g_nodes × (g_nodes - 1)) / 2)Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that node values are the labels themselves, 1 through g_nodes. Build a Union-Find over all nodes, union each edge pair, then track the min and max per root. Answer is the largest max minus min across roots. The edge case that breaks naive code: nodes absent from every edge. They're their own component with difference 0, so they never raise the answer, but your loops must still initialize them correctly or you'll index into empty structures. Another pitfall is recursion depth. With up to 10^5 nodes, a recursive DFS on a long chain can overflow the stack, so use iterative DFS or Union-Find with path compression. Also watch the answer when no edges connect anything useful: it should be 0, not a negative sentinel. If the assessment clock is tight and you freeze, StealthCoder is the safety net that can produce a clean iterative version.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Maximum Difference 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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Akuna Capital's OA.
Akuna Capital reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Maximum Difference FAQ
How hard is the Akuna Capital Maximum Difference problem really?+
Easy to medium. It's standard connected components with a min and max tracked per group. If you've written Union-Find or BFS once, you can finish it quickly. The difficulty is almost entirely in the edge cases and the input size, not the idea.
What's the trick to solve Maximum Difference?+
Union all edge pairs, then for each component record the smallest and largest node. Return the biggest gap. Treat every node from 1 to g_nodes as its own component first, so isolated nodes are handled automatically with difference 0.
Should I use DFS or Union-Find?+
Either works. Union-Find is shorter and avoids recursion depth issues on long chains. If you prefer DFS, write it iteratively with an explicit stack, since g_nodes can reach 10^5 and a recursive version may crash on a path-shaped graph.
What edge cases should I test before submitting?+
Test a graph where some nodes have no edges, a single connected chain covering all nodes, and a case where the biggest gap is in a small component like nodes 1 and 5. Also check that the result is 0 when every component is a single node or a pair of adjacent values.
How do I prepare for this in 48 hours?+
Write Union-Find from memory twice, with path compression, and add min/max arrays updated on union. Then run the three given examples by hand. That covers this problem and most graph grouping questions you might see in a similar assessment.