Find Median Of Subarray Uniqueness
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon's median-of-subarray-uniqueness problem hits in April 2024, and it's testing whether you can track unique element counts across windows without getting bogged down in naive recalculations. The OA will give you an array and ask you to find the median of the uniqueness counts for every contiguous subarray. Most candidates blank on the efficiency part, but the pattern is sliding window plus a data structure to track frequencies fast. If you freeze during the live assessment, StealthCoder reads the problem and surfaces the approach in real time.
Pattern and pitfall
The core trick: for each subarray, count how many unique elements it contains, then collect all those counts and find the median. A brute-force loop through subarrays works for small inputs, but you'll hit time limits. The real solution uses a sliding window with a hash map or frequency counter to track unique element counts as you expand and contract the window. The median itself requires you to sort or maintain a balanced data structure of the uniqueness values. Common pitfall: candidates recalculate uniqueness from scratch for every window instead of maintaining state. This is where a two-pointer or prefix-sum approach combined with a multiset or sorted list shines. StealthCoder acts as your safety net if the window-management logic gets fuzzy under pressure.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Find Median Of Subarray Uniqueness 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 would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Find Median Of Subarray Uniqueness FAQ
Is this a sliding window problem or something else?+
It's primarily sliding window for tracking unique counts efficiently, but you also need to collect medians. That's why many solutions pair the window with a balanced data structure like a multiset or heap to extract the median quickly without repeated sorting.
Do I really need to consider every subarray?+
Yes. The problem asks for the median of all uniqueness counts across all contiguous subarrays. You can't skip any. The optimization is in how you compute uniqueness counts per window, not in which windows you check.
What's the median extraction trick here?+
After you collect all uniqueness counts, sort them and pick the middle value. If you're updating counts incrementally, use a balanced BST or two heaps to avoid sorting from scratch each time. That's what separates O(n^2) from O(n log n).
How do I handle ties or even-length arrays when finding the median?+
For an array of uniqueness counts with even length, most problems take the average of the two middle elements or the lower/upper middle. Check the problem statement. If it's not specified, lower middle is a common default in competitive coding.
Can I solve this in 48 hours without deep prep?+
If you've seen sliding window and hash maps before, the logic is reachable. The bottleneck is coding the window-tracking logic cleanly and choosing the right median data structure. Practice one similar problem on LeetCode beforehand to cement the window pattern.