Reported June 2026
Amazonmonotonic stack

Count Promotional Periods

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

The mistake that sinks a first attempt at this Amazon OA, reported in June 2026, is writing the obvious double loop over every (i, j) pair and watching it die at n = 2 x 10^5. Count Promotional Periods looks like a subarray problem, but it's really a monotonic stack problem in disguise. You need to count pairs of days where both endpoints beat everything between them. If you've never seen that shape, it's easy to freeze. StealthCoder is the safety net that runs invisibly during the live OA if your mind goes blank, but the trick below is short enough to carry in your head.

The problem

Data analysts at Amazon are studying product order patterns. They classify a period of at least three consecutive days as a promotional period when the order counts on the first and last days are both greater than every order count on the days between them.
More formally, for an array orders, a subarray from index i to index j is a promotional period if j - i + 1 >= 3 and min(orders[i], orders[j]) > max(orders[i + 1], orders[i + 2],..., orders[j - 1]).
Given the order statistics for n consecutive days, return the number of promotional periods.

Function
countPromotionalPeriods(orders: int[]) → long
Complete the function countPromotionalPeriods.
countPromotionalPeriods has the following parameter:
int orders[n]: the order statistics for each day
Returns long: the number of promotional periods.

Examples
Example 1
orders = [3, 2, 8, 6]
return = 1
Using 1-based indexing, the candidate periods of length at least 3 are [1, 3], [1, 4], and [2, 4]. Period [1, 3] is valid because min(3, 8) = 3 and the only middle value is 2. The other two periods are invalid because their middle maximum is 8. Therefore, the answer is 1.
Example 2
orders = [5, 1, 4, 2, 6]
return = 3
The promotional periods are [5, 1, 4], [4, 2, 6], and [5, 1, 4, 2, 6].

Constraints
3 <= n <= 2 x 10^5
1 <= orders[i] <= 10^9
All integers in orders are distinct.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Count pairs (i, j) with j - i >= 2 where every element between them is smaller than both endpoints. Use a monotonic decreasing stack. Scan left to right. For each new value x, pop while the top is smaller than x. Each popped element is a valid left endpoint candidate that x can see, so count pairs carefully. After popping, if the stack is non-empty, the new top also forms a pair with x. The pitfall is adjacent pairs: j - i + 1 >= 3 means at least one middle element, so an adjacent pair doesn't count. Subtract those, or only count a pair when something was popped between. Distinct values remove the tie headache, which is why the constraint is stated. Use a 64-bit counter since the answer can exceed int range. Total work is O(n) because each element is pushed and popped once. StealthCoder can back you up live, but practice the pop logic on example 2 first.

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 Count Promotional Periods 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

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

Count Promotional Periods FAQ

How hard is Count Promotional Periods really?+

Medium-hard if you haven't seen monotonic stacks. The brute force is trivial, but n up to 2 x 10^5 kills it. Once you recognize the stack pattern, the code is about ten lines. The hard part is counting pairs correctly without including adjacent days.

What's the trick to getting O(n)?+

Keep a stack of values in decreasing order. When a new value pops smaller elements, those popped values were the middle elements, and the element left beneath each pop is a valid left endpoint. Each element enters and leaves once, so the whole scan is linear.

Why does the answer need a long?+

The number of valid pairs can grow roughly quadratically in the worst case, which overflows a 32-bit int for large n. Use a 64-bit accumulator from the start. In Python it doesn't matter, but in Java or C++ it will fail hidden tests.

Do I need to worry about duplicate values?+

No. The constraints say all integers in orders are distinct, so you never have to decide whether equal values pop or stay. That simplifies the comparison to strictly greater or less. Don't add tie handling you don't need.

How do I prepare for this in 48 hours?+

Trace example 2, [5, 1, 4, 2, 6], by hand with a stack and confirm you get 3. Then code it and test an increasing array and a decreasing array. Watch for off-by-one errors on adjacent pairs, since those need at least one middle day.

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