Reported August 2023
Amazonarray

Count Spikes

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

You're looking at an Amazon OA from August 2023 that asks you to count k-Spikes in an array. A spike at index i needs at least k smaller elements to its left AND at least k smaller elements to its right. This is a classic two-pass problem that trips up candidates who try to solve it in one pass or who miscalculate the boundary conditions. StealthCoder will catch you if you blank on the prefix/suffix logic during your live assessment.

The problem

\ A k-Spike is an element that satisfies both the following conditions: There are at least k elements from indices (0, i-1) that are less than prices[i]. There are at least k elements from indices (i+1, n-1) that are less than prices[i]. Count the number of k-Spikes in the given array.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The pattern is straightforward: precompute two arrays. First pass left-to-right counts how many elements smaller than prices[i] exist from index 0 to i-1. Second pass right-to-left does the same for i+1 to n-1. Then iterate through and count indices where both counts are >= k. The trick candidates miss is off-by-one errors in the index ranges and confusing 'at least k' with 'exactly k'. Building the left and right arrays takes O(n log n) if you use a sorted structure, or O(n^2) if you brute force. The counting step is O(n). Most submissions timeout because they try to count comparisons inside the spike-checking loop instead of precomputing. StealthCoder lets you paste a working template if the logic evaporates under pressure.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Count Spikes 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 by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

Amazon reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Count Spikes FAQ

What's the difference between 'at least k elements less than prices[i]' and 'k elements less than prices[i]'?+

'At least k' means >= k. If k=2 and there are 5 smaller elements to the left, that counts. This catches candidates who code for 'exactly k' or who misread the problem and search for the kth smallest instead.

Do the left and right ranges overlap or touch the spike index?+

No. Left range is (0, i-1). Right range is (i+1, n-1). Index i itself is never included. If you include i, you're solving the wrong problem and will get wrong answers on basic test cases.

Can I solve this in a single pass?+

No. You need to know the future (right-side counts) and the past (left-side counts) before deciding if index i is a spike. Two passes are mandatory. Single-pass attempts fail on most inputs.

What's the fastest way to count smaller elements without timing out?+

For each index, iterate through the range and count. That's O(n^2) but acceptable for n < 10,000. Merge-sort or balanced BST approaches are O(n log n) but overkill unless n is huge. Start with the simple version.

How do I verify my solution on paper before submitting?+

Pick a small array like [1,3,1,4,1,5] with k=1. Manually compute left counts [0,1,1,2,2,3] and right counts [2,2,1,1,1,0]. Check: index 1 has 1 left, 2 right (spike); index 3 has 2 left, 1 right (spike). Count=2. Trace your code on this.

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