Reported April 2024
Amazonbinary search

Kth Smallest in Subarray

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

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

Amazon's April OA threw this at candidates: find the kth smallest element in a subarray. It sounds simple until you realize you're either doing this for every possible subarray or you're missing the optimization entirely. The pattern is binary search on the answer space, not on an array. StealthCoder helps here because the insight isn't obvious, and blanking on it costs you the whole problem. This one separates candidates who can think beyond naive iteration.

Pattern and pitfall

The trap is trying to enumerate subarrays and sort each one. That's O(n^2 log n) minimum. The real move is binary search on the value range: for a given candidate answer x, count how many elements in the subarray are less than or equal to x. If that count is less than k, the kth smallest must be larger. Adjust your search bounds and repeat. You're not searching an array; you're searching the possible value space. The subarray part might be fixed by the input, or you might need to handle a sliding window variant. Either way, binary search on answer space is the unlock. If you freeze during the OA, StealthCoder can walk you through the bounds and the counting logic without the proctor seeing it.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Kth Smallest in Subarray 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 StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as kth smallest element in a sorted matrix. If you have time before the OA, drill that.

⏵ The honest play

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

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

Kth Smallest in Subarray FAQ

Is this really a binary search problem?+

Yes, but not in the traditional way. You're binary searching the range of possible values, not an array index. For each candidate value, count elements in the subarray that are less than or equal to it, and adjust your bounds. This is O(n log(max value)) instead of O(n^2 log n).

What if the subarray is a sliding window?+

Then you run the binary search algorithm for each window position. Maintain or recompute the count of elements less than or equal to the candidate value as the window moves. Some solutions use a multiset or sorted structure per window for faster counting.

How do I count elements less than or equal to x in a subarray?+

Iterate once through the subarray and count. You can also use a sorted data structure like a multiset (C++) or TreeMap (Java) to support efficient rank queries. The simplest approach is O(n) per query, which is still acceptable if queries are not repeated.

What are the binary search bounds?+

Lower bound is the minimum element in the subarray (or the array if unknown). Upper bound is the maximum element in the subarray. The kth smallest must lie within this range. Converge until your bounds are tight.

Did Amazon really ask this in April 2024?+

Yes, multiple candidates reported it that month. Variations exist: fixed subarray, sliding window, or queries on different subarrays. The core trick is always binary search on value space, not index space.

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

OA at Amazon?
Invisible during screen share
Get it