Reported July 2026
eBayheap priority queue

K Smallest Elements from Sorted Arrays

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

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

The eBay OA reported in July 2026 looks like a sorting problem, but it's really a k-way merge with an early exit. You get up to 10000 sorted arrays and need the k smallest values across all of them. Dumping everything into one list and sorting is the trap, and the problem text tells you not to push every value into a heap. Keep one candidate per nonempty array, pop the smallest, push that array's next value, and stop after k pops. If you blank on the heap bookkeeping, StealthCoder runs invisibly during the live OA and gives you a working solution as a safety net.

The problem

You are given an array of integer arrays arrays. Each inner array is sorted in nondecreasing order. Return the k smallest values across all inner arrays in nondecreasing order.
Duplicate values occupy separate positions and must be retained. An empty inner array contributes no values. When k is zero, return an empty array.
Do not insert every input value into a heap. Maintain at most one current candidate from each nonempty inner array.

Function
kSmallestElements(arrays: int[][], k: int) → int[]

Examples
Example 1
arrays = [[1,4,7],[2,5],[3,6,9]]
k = 5
return = [1,2,3,4,5]
The first five values in the merged order are 1, 2, 3, 4, 5.
Example 2
arrays = [[],[-3,-1,2],[-3,4]]
k = 4
return = [-3,-3,-1,2]
Both occurrences of -3 are retained, and the empty array is ignored.

Constraints
1 <= arrays.length <= 10000
0 <= arrays[i].length
The total number of values across all arrays is at most 200000.
Each inner array is sorted in nondecreasing order.
-2147483648 <= arrays[i][j] <= 2147483647
0 <= k <= the total number of values.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is a min-heap seeded with the first element of each nonempty array. Store (value, arrayIndex, elementIndex) in each entry. Pop the smallest, append it to the result, then push the next element from the same array if one exists. Repeat until you have k values or the heap empties. That costs O(n + k log m), where m is the number of arrays and n is the total value count. The common pitfalls are all edge cases. k equals zero should return an empty array immediately. Empty inner arrays must be skipped at seed time. Duplicates stay, so don't dedupe. Values hit the full 32-bit range, so don't use a sentinel like Integer.MAX_VALUE, and be careful with subtraction in comparators because it overflows. If the heap logic gets tangled mid-assessment, StealthCoder is the hedge that hands you the clean version.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill K Smallest Elements from 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 StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass eBay's OA.

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

K Smallest Elements from Sorted Arrays FAQ

What's the trick in the eBay K Smallest Elements problem?+

Treat it as a k-way merge. Put the first value of each nonempty array in a min-heap, pop the smallest, then push the next value from that same array. Stop after k pops. You never need more than one candidate per array in the heap.

How hard is this one really?+

Medium. The idea is standard if you've seen merge k sorted lists. The difficulty is in the details: tracking array and element indexes, skipping empty arrays, handling k equal to 0, and avoiding integer overflow in comparisons.

Why not just flatten and sort everything?+

It works for correctness but costs O(n log n) on up to 200000 values and ignores the sorted structure. The problem explicitly says not to insert every value into a heap. The merge approach does O(n + k log m) work, which is much better when k is small.

What edge cases should I test before submitting?+

Test k equal to 0, empty inner arrays, all arrays empty with k at 0, duplicates across arrays like the two -3 values, and extreme values near -2147483648 and 2147483647. Also test k equal to the total count so the heap drains fully.

How do I prepare in 48 hours for this?+

Write a k-way merge with a heap twice from scratch. Use tuples or small objects with a comparator that compares values directly, not by subtraction. Then run the two examples and your edge cases. That covers the pattern and most of the bugs.

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

OA at eBay?
Invisible during screen share
Get it