Reported July 2026
Appleunion find

Group Transitive String Aliases

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

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

Apple reportedly served this one in July 2026, and the constraint is what gives it away: up to 10^5 pairs means you can't rescan the whole list every time you merge two aliases. Group Transitive String Aliases looks like string busywork, but it's a connected components problem in disguise. Map strings to IDs with a hash table, merge them with union-find, then bucket and sort. If you've seen account merging, you've seen this. If you blank under the timer, StealthCoder runs invisibly on your screen as a safety net during the live OA.

The problem

You are given an array pairs, where each row contains two string aliases. Alias relationships are bidirectional and transitive: if a is an alias of b, and b is an alias of c, then all three strings belong to the same alias group.
Return every connected alias group. Within each group, sort all words lexicographically; the first word is therefore the key that the source associates with that group. Return the groups in lexicographic order by their first words.

Function
groupAliases(pairs: String[][]) → List<List<String>>

Examples
Example 1
pairs = [["a","b"],["b","c"],["d","e"]]
return = [["a","b","c"],["d","e"]]
The first two pairs connect a, b, and c. The third pair forms the separate group [d,e].
Example 2
pairs = [["z","x"],["m","m"],["x","y"],["z","x"]]
return = [["m"],["x","y","z"]]
A self-pair keeps m as a one-word group. Repeated pairs do not change connectivity.

Constraints
1 <= pairs.length <= 10^5
pairs[i].length == 2
Every alias is a non-empty printable-ASCII string of length at most 30.
Duplicate pairs and self-pairs are allowed.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is treating every alias as a node and every pair as an edge. Use a hash map from string to integer ID, then union-find with path compression to merge. Self-pairs just register a node, and duplicates are harmless because union on already-joined nodes does nothing. After processing, loop through every known word, find its root, and append it to that root's bucket. Sort each bucket lexicographically, then sort the buckets by their first word. The common pitfall is the brute-force approach: merging sets by scanning all existing groups per pair, which goes quadratic and dies at 10^5. Another miss is forgetting that a self-pair like [m,m] still creates a group. Also sort with plain string comparison, not length first. With up to 2*10^5 distinct words, sorting dominates at O(n log n). If the DSU details slip away mid-assessment, StealthCoder can hand you a working version in real time.

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 Group Transitive String Aliases 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 Apple's OA.

Apple 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.

Group Transitive String Aliases FAQ

What's the trick to Group Transitive String Aliases?+

Model it as connected components. Assign each unique string an integer ID using a hash map, union the two IDs for every pair, then group words by their root. Sort inside each group and sort the groups by first word. That's the whole problem.

Should I use union-find or DFS for this Apple OA question?+

Either works, but union-find is shorter and safer here. DFS needs an adjacency map and an iterative approach to avoid deep recursion on long alias chains. Union-find with path compression handles 10^5 pairs cleanly and has less code to get wrong.

How do self-pairs and duplicate pairs affect the answer?+

A self-pair like [m,m] still registers m as a node, so it must appear as its own single-word group if nothing else connects to it. Duplicate pairs just repeat a union that already happened, so they change nothing. Register both words before unioning.

What's the time complexity I should aim for?+

Near-linear for the merging, roughly O(n * alpha(n)) with union-find, plus O(n log n) for sorting words and groups. With up to 10^5 pairs and strings of length 30, that's comfortable. Anything that rescans all groups per pair will be too slow.

How do I prepare for this in 48 hours?+

Write union-find from memory until it takes five minutes, including path compression. Then do one account-merging style problem that uses string keys. Practice the final step too: bucket by root, sort each bucket, sort buckets by first element. That's where people drop points.

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

OA at Apple?
Invisible during screen share
Get it