Get Success Value
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
You're looking at an Amazon OA from December 2023 that sounds simple but has a timing trap. The scenario is Prime Video viewership across regions, and you need to return the sum of the top k viewers for multiple queries. If you see this live and your mind goes blank on the optimal approach, StealthCoder will show you the pattern instantly. The trick isn't the logic. It's knowing when to sort once versus sorting per query.
The problem
Amazon Prime Video has recently released an exclusive series on its platform. They collected the number of viewers from n different regions across the world and stored the data in the array num_viewers. The success value of the release is defined as the sum of viewership in the top k regions, those with the highest viewers. For example, if num_viewers = [3, 2, 1, 4, 5] and k = 3, then the success value of the release is 3 + 4 + 5 = 12 as [3, 4, 5] are the top 3 values. Given a number of k values, calculate the success value for each query. Function Description Complete the function getSuccessValue in the editor. getSuccessValue has the following parameters: Returns int[q]: the maximum possible success values for each query
Reported by candidates. Source: FastPrep
Pattern and pitfall
This is a top-k sum problem. The naive approach sorts for every query, which kills you when q is large. The smarter move: sort num_viewers once, then for each query with a given k, sum the last k elements (or first k if you reverse). Time complexity matters here. A single sort is O(n log n), then each query is O(k). If you try to sort per query with q large, you're at O(q * n log n), which will time out. The pattern is sorting plus prefix logic. StealthCoder catches candidates who implement the naive solution and helps pivot to the one-sort approach before submission.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Get Success Value 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as top k frequent elements. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Get Success Value FAQ
Do I need to sort for every query or just once?+
Sort once at the start. Then for each k value, sum the top k elements from the sorted array. Sorting per query is the classic mistake that causes timeout. Amazon specifically looks for candidates who optimize this.
What if k is larger than the array size?+
The problem doesn't explicitly state bounds, but assume k is always valid (1 to n). If not, clamp k to n or sum the entire array. Test this assumption during the OA if unclear from the input format.
Should I use a heap instead of sorting?+
A heap (priority queue) is overkill here. Sorting the array once is cleaner and faster for multiple queries. Heaps shine when you're updating the dataset or only need the top 1 or 2 elements repeatedly.
How do I structure the input parsing for multiple k values?+
The problem mentions 'q queries' but doesn't show the exact input format. Assume you receive an array of k values, one per query. Parse that, then return an integer array with one success value per query.
Is this still asked at Amazon in 2024?+
Top-k sum problems are evergreen at Amazon because they test both sorting knowledge and optimization thinking. December 2023 is recent enough that the pattern hasn't shifted. Expect it.