Merge Subsequences Into a Consistent Total Order
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The edge case that kills most first attempts at this Google OA, reported in September 2026, is the tie. Two values with no constraint between them, and your output has to pick one. You're handed several subsequences, every adjacent pair is a directed edge, and you must merge all distinct values into one sequence that honors every edge. It's a topological sort wearing a sorting hint. The example expects 3 before 4 when both are available, so the tiebreak matters. If you blank on the structure during the live OA, StealthCoder runs invisibly on your desktop as a safety net.
The problem
You are given several integer subsequences. Every adjacent pair in a subsequence imposes a directed ordering constraint. Merge all distinct values into one total sequence that respects every constraint. Examples Example 1 subsequences = [[1,2,3],[1,3],[2,4]] return = [1,2,3,4] The order respects every edge and chooses 3 before 4 when both are available.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Build a directed graph from every adjacent pair in every subsequence. Track in-degrees for all distinct values, including ones that appear only once and have no edges. Then run Kahn's algorithm. The trick is the tiebreak: in Example 1, after 2 is placed, both 3 and 4 become available, and the expected output puts 3 first. That points to a min-heap instead of a plain queue, so the smallest available value always goes next. Pitfalls: duplicate edges inflating in-degree (use a set per node), forgetting isolated values, and a cycle, where the output is shorter than the distinct count. Decide what to do there, since the input doesn't say. Complexity is O(E log V). If you freeze in the live OA, StealthCoder can surface the heap-based Kahn's approach so you can type it with confidence.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Merge Subsequences Into a Consistent Total Order 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 StealthCoderRelated leaked OAs
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.
Merge Subsequences Into a Consistent Total Order FAQ
What's the actual trick in this Google OA question?+
It's a topological sort. Each adjacent pair in a subsequence is a directed edge, and you emit nodes whose prerequisites are done. The sorting hint refers to the tiebreak: when several values are available, pick the smallest. That's why a min-heap beats a plain queue here.
Why does a naive solution fail on the edge case?+
Naive merging concatenates or sorts values and ignores constraints, or it uses a plain queue and gets a valid but different order. Example 1 expects 3 before 4 when both are available, so ties need a deterministic rule. Duplicate edges also corrupt in-degree counts if you don't dedupe.
Do I need a heap or is a queue enough?+
If the expected output requires the smallest available value first, use a min-heap. A queue gives a valid topological order, but not necessarily the one the tests expect. Based on Example 1, assume the tiebreak matters and use the heap.
How should I handle values that appear in only one subsequence?+
Add every distinct value as a node up front with in-degree zero. A value in a single-element subsequence has no edges but still belongs in the output. Skipping it is a common reason for a short result.
How do I prepare for this in 48 hours?+
Write Kahn's algorithm from scratch twice, once with a queue and once with a heap. Practice building the graph from adjacent pairs with deduped edges. Test on a cycle and on isolated nodes. That covers nearly everything this problem can throw at you.