Stack with Constant-Time Middle Queries
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon reported this one in September 2026, and the detail that matters is the lower middle on an even-sized stack. Every operation has to run in constant time, so you can't scan to find the center. It's a stack with a twist, and the twist is tracking one extra index. If you've got the OA in a day or two, this is a pattern worth locking in. And if you blank mid-assessment, StealthCoder runs invisibly on your desktop as a safety net while you work through it.
The problem
Process a sequence of stack operations. Each operation is ["push", value], ["pop"], ["top"], or ["middle"]. Values are signed decimal integers encoded as strings. Return one string for every non-push operation. For an empty stack, return EMPTY. For an even-sized stack, middle returns the lower middle element, the one closer to the bottom. Every operation must run in constant time; preallocated array storage is allowed. Function processMiddleStack(operations: String[][]) → String[] Examples Example 1 operations = [["push","1"],["push","2"],["push","3"],["middle"],["top"],["pop"],["middle"]] return = ["2","3","3","1"] The lower middle of three values is 2; after popping 3, the lower middle of [1,2] is 1. Example 2 operations = [["top"],["middle"],["pop"]] return = ["EMPTY","EMPTY","EMPTY"] All three queries use the empty sentinel. Example 3 operations = [["push","-4"],["middle"],["pop"]] return = ["-4","-4"] A one-element stack has the same top and middle. Constraints 1 ≤ operations.length ≤ 100000. Every operation has one of the four documented forms. Push values are integers in [-10^9, 10^9]. The output contains one entry for every non-push operation.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that you don't need a linked list or a deque. Store values in a plain array, since preallocated storage is allowed, and keep a size counter. The lower middle sits at index (size-1)/2 using integer division. Push, pop, top and middle each become a single array lookup or counter change, so everything is O(1). Check the examples: size 3 gives index 1, which is 2. Size 2 gives index 0, which is 1. That matches. The common pitfall is returning the upper middle on even sizes, or forgetting that pop and top on an empty stack must output EMPTY while a push outputs nothing. Also keep values as strings in the output. Parse nothing unless you need to. If the index math slips under pressure, StealthCoder is the hedge during the live OA, but the logic is about five lines.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Stack with Constant-Time Middle Queries 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
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.
Stack with Constant-Time Middle Queries FAQ
How hard is the Amazon stack middle query problem really?+
Easy once you see it. The whole problem is index math on an array. The (size-1)/2 formula gives the lower middle, and every operation is a lookup. The difficulty is in reading the spec carefully, not in the algorithm.
What's the trick to getting constant time for middle?+
Don't search for the middle. Keep an array and a size counter, then read index (size-1)/2. Push writes at index size and increments. Pop decrements. Nothing ever scans or shifts, so all four operations stay O(1).
Which middle do I return when the stack has an even number of elements?+
The lower middle, the one closer to the bottom. For [1,2] that's 1, at index 0. With integer division, (2-1)/2 equals 0, so the same formula works for odd and even sizes without a special case.
What should the output look like for empty stack operations?+
Top, middle and pop on an empty stack each add the string EMPTY to the result. Push adds nothing. Also note pop on a non-empty stack returns the removed value, as Example 1 shows, where the pop outputs 3.
How do I prepare for this in 48 hours?+
Write the array-plus-counter version once from memory and run all three examples by hand. Then test edge cases: empty stack, one element, and pop down to empty then push again. That covers nearly every way this problem breaks.