Paginated K-Way Unique Merge
Reported by candidates from Airwallex's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The data structure this Airwallex problem hinges on is a min-heap, and the September 2026 report says it's wrapped in a paginated-fetch story to make it look harder than it is. You've got k sorted streams, a page size, and a dedupe requirement. That's a k-way merge with a heap. If your OA lands in the next day or two, learn the shape now: push each source's head, pop the smallest, skip repeats, refill from the stream. StealthCoder is the safety net if the pagination detail makes you blank mid-assessment, but the core idea is short enough to hold in your head.
The problem
Each row in sources is one data source's nondecreasing record-id stream. A source can be fetched only in consecutive pages of at most pageSize records. Merge all sources into one strictly increasing sequence, removing duplicate ids within or across sources. Process records in streaming order; the output models calls to a writer that accepts one id at a time. Function mergePagedSources(sources: int[][], pageSize: int) → int[] Examples Example 1 sources = [[1,4,7],[1,2,7,9],[3,4,8]] pageSize = 2 return = [1,2,3,4,7,8,9] The sorted streams merge and duplicate 1, 4, and 7 appear once. Example 2 sources = [[],[2,2,2],[1,3]] pageSize = 1 return = [1,2,3] Empty sources and duplicates are supported. Example 3 sources = [[5,6],[1,2,3]] pageSize = 10 return = [1,2,3,5,6] A short page exhausts each source. Constraints 0 <= sources.length <= 10^4. Every source is nondecreasing; the total record count is at most 2 * 10^5. 1 <= pageSize <= 10^5.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Put one entry per non-empty source into a min-heap, keyed by (value, source index, position). Pop the smallest. If it equals the last value you wrote, drop it. Otherwise append it to the output. Then advance that source's pointer and push its next record. Pagination is a wrapper: keep a per-source buffer of at most pageSize records and fetch the next page only when the buffer runs out. For the output, the page size changes nothing. The common pitfall is deduping only within a source and not across sources. Comparing to the last emitted id fixes that, since the heap pops in nondecreasing order. Another trap is dumping everything into one array and sorting. That works for correctness but ignores the streaming framing. Complexity is O(N log k) with N up to 2*10^5 and k up to 10^4. Watch empty sources and repeated ids like [2,2,2]. If you freeze on the heap tuple or the refill logic, StealthCoder can cover you live in the OA.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Paginated K-Way Unique Merge 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 StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Airwallex's OA.
Airwallex 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.
Paginated K-Way Unique Merge FAQ
What's the trick in Paginated K-Way Unique Merge?+
It's a k-way merge with a min-heap. Seed the heap with each source's first record, pop the smallest, and push that source's next record. Dedupe by comparing against the last id you emitted. Pagination only affects how you buffer and refill each source.
Does pageSize change the algorithm?+
Not the output. The merged result is identical for any pageSize. It only dictates how many records you pull per fetch. Model it as a per-source buffer that refills when empty, and the heap logic stays the same.
Can I just concatenate and sort?+
It gives the right answer, but it's O(N log N) and ignores the streaming setup. With N up to 2*10^5 it would likely pass on correctness, but the heap approach shows you read the problem. Use the heap if you can write it cleanly.
How do I handle duplicates across sources?+
Track the last id written. Because the heap pops values in nondecreasing order, any duplicate, within a source or across sources, will be adjacent in pop order. If the popped value equals the last written one, skip it and still advance that source.
How do I prepare for this in 48 hours?+
Write a k-way merge with a heap from scratch twice, once without dedupe and once with it. Then test empty sources, all-duplicate sources like [2,2,2], and zero sources. That covers the edge cases this problem is built around.