Merge Three Sorted Arrays
Reported by candidates from SambaNova Systems's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The SambaNova Systems OA reported in June 2022 hands you three sorted arrays and asks for one merged, deduplicated result. It looks like a warm-up, and it is. But the quick version has a trap: duplicates can show up within one array and across all three, and one array can be empty. If you're taking this OA in the next day or two, the pattern is a three-pointer merge on plain arrays. Get the dedupe check right and you're done. StealthCoder sits as a safety net on the live OA if your mind goes blank on the pointer logic.
The problem
You are given three integer arrays that are each sorted in non-decreasing order. Merge the arrays into one sorted array and remove duplicate values. Return the merged array in non-decreasing order. Function mergeThreeSortedArrays(a: int[], b: int[], c: int[]) → int[] Complete the function mergeThreeSortedArrays in the editor. mergeThreeSortedArrays has the following parameters: int a[]: the first sorted array int b[]: the second sorted array int c[]: the third sorted array Returns int[]: the sorted merged array with duplicates removed Examples Example 1 a = [1, 3, 5] b = [1, 2, 5, 6] c = [2, 4, 6] return = [1, 2, 3, 4, 5, 6] After merging all values and removing duplicates, the sorted result is [1, 2, 3, 4, 5, 6]. Example 2 a = [] b = [0, 0, 1] c = [1, 2] return = [0, 1, 2] Empty arrays are allowed. Duplicate 0 and 1 values are returned once. Constraints Each input array is sorted in non-decreasing order. The arrays may contain duplicate values. The arrays may be empty.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The clean approach is three pointers, one per array. At each step, pick the smallest of the values at the pointers that are still in range. Compare it to the last value you appended. Only append if it's different, then advance that pointer. Loop until all three pointers run off the end. That dedupes in one pass, O(n) time, with no sort and no set. The edge case that breaks naive solutions: empty arrays, like a = [] in Example 2, and repeated values like [0, 0, 1] where the dupes sit inside a single array. Checking only against the other arrays misses those. Also watch an empty result list when you compare to the last element. Guard that. Concatenating and sorting works but ignores the sorted input. If you blank on the bounds handling during the live OA, StealthCoder is the hedge that gets you a working merge.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Merge Three Sorted Arrays 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 SambaNova Systems's OA.
SambaNova Systems 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.
Merge Three Sorted Arrays FAQ
How hard is Merge Three Sorted Arrays really?+
Easy. It's the merge step from merge sort, extended to three inputs, plus a dedupe rule. Most of the risk is careless handling of empty arrays and duplicates, not the algorithm. If you can write a two-array merge, you can write this one.
What's the trick to removing duplicates?+
Compare each candidate to the last value you appended to the result. Because inputs are sorted, all equal values arrive next to each other in the merged order. So one comparison against the result's tail catches every duplicate, inside one array or across arrays.
Can I just concatenate and sort?+
Yes, and it's correct. Concatenate, sort, then dedupe adjacent values. It costs O(n log n) instead of O(n). It will likely pass, but the three-pointer merge is cleaner and shows you used the sorted property. Know both so you can pick the faster one to write.
What edge cases should I test before submitting?+
Test one empty array, like a = [] with b = [0, 0, 1] and c = [1, 2]. Test all three empty, which should return an empty list. Test duplicates inside a single array and the same value in all three arrays. Those cover the usual failures.
How do I prepare for this in 48 hours?+
Write a two-pointer merge from scratch, then extend it to three pointers with a helper that handles exhausted arrays. Add the last-appended dedupe check. Run it on both examples and an all-empty case. That's about an hour of work and covers the question.