Find Sum of Beauties
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon asked this in September 2024, and candidates reported blanking on the indexing logic. Find Sum of Beauties is a straightforward array traversal problem that looks simpler than it is. The trap is in how you compute the beauty metric for each element. You need to know whether smaller and larger elements exist in the correct positions around each index. If you freeze during the OA, StealthCoder will feed you the two-pass approach in real time. It's the safety net for when pattern recognition fails under pressure.
Pattern and pitfall
The beauty of an element at index i is the count of indices j < i where arr[j] < arr[i], plus the count of indices k > i where arr[k] > arr[i]. Brute force is O(n squared): check every pair. The trick is to precompute two arrays in O(n) passes: left-smaller[i] tells you how many elements to the left are smaller, and right-larger[i] tells you how many to the right are larger. Sum them for each index. This pattern appears often in Amazon assessments because it tests whether you optimize beyond nested loops. StealthCoder will surface the two-pass template if you're stuck mid-OA.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Find Sum of Beauties 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 Sum of Beauties FAQ
Is this a hard problem?+
No. The core logic is simple once you see it: count smaller/larger neighbors. The difficulty is recognizing you need two passes instead of brute force. Amazon loves this tier of problem because it separates optimization thinking from brute force coding.
What's the time complexity I need to hit?+
O(n). Brute force O(n squared) will likely time out. Build prefix counts: iterate left-to-right counting smaller elements, then right-to-left counting larger ones. Two linear scans, one answer array.
Can I use a HashMap or heap for this?+
Not necessary. Two simple loops with counters work fine. Overcomplicating with a TreeMap or segment tree wastes time on the OA. Stick with the two-pass array approach.
What if the array has duplicates?+
Check the exact problem statement for the equality rules. Typically, duplicates do not count as 'smaller' or 'larger'. Be strict about the comparison operators you use. Off-by-one errors on duplicates sink candidates often.
How do I prepare in 24 hours?+
Understand the two-pass template. Code it once, trace it on a small example (5 elements), verify the beauty sum. Don't memorize variations. On the OA, read the problem twice before coding. Index logic is where you lose points.