Reported December 2021
Bloombergstack

Max Stack

Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Bloomberg OA. Under 2s to a working solution.
Founder's read

The whole Bloomberg Max Stack question, reported in December 2021, comes down to one choice: how you store the stack so POP_MAX doesn't cost O(n). It's a stack problem with a twist, because you have to delete the topmost maximum from the middle. Up to 100000 operations means a naive scan per POP_MAX will die. If you've seen the classic min stack, this is its harder cousin. StealthCoder sits invisibly on your screen during the live OA as a safety net if you blank, but know the shape of the answer before you sit down.

The problem

Implement a max stack by processing the finite array operations from left to right. The stack starts empty.
Each operation has one of these forms:
PUSH value: push value onto the top of the stack.
POP: remove and return the top value.
PEEK: return the top value without removing it.
PEEK_MAX: return the largest value currently in the stack without removing it.
POP_MAX: remove and return the largest value. If the maximum occurs more than once, remove the occurrence closest to the top.
Return the values produced by every operation except PUSH, in command order.

Function
runMaxStack(operations: String[]) → int[]

Examples
Example 1
operations = ["PUSH 5","PUSH 1","PUSH 5","PEEK","POP_MAX","PEEK","PEEK_MAX","POP","PEEK"]
return = [5,5,1,5,1,5]
POP_MAX removes the upper copy of 5. The stack is then [5,1] from bottom to top.
Example 2
operations = ["PUSH 2","PUSH 2","PUSH 1","POP_MAX","PEEK","POP","PEEK_MAX"]
return = [2,1,1,2]
The topmost maximum is the second pushed 2, even though 1 is above it. Removing that node leaves 1 on top.

Constraints
1 <= operations.length <= 100000.
Every operation is exactly one documented command; PUSH has one separating space before its value.
-10^9 <= value <= 10^9.
Every POP, PEEK, PEEK_MAX, and POP_MAX is issued while the stack is nonempty.
At least one operation produces output.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is that a plain stack with a running max can't handle POP_MAX, because removing a middle element breaks the tracking. Use a doubly linked list for the stack order plus a sorted structure keyed by value, where each value maps to a list of nodes ordered by push time. PEEK_MAX reads the largest key and POP_MAX takes the latest node under it, then unlinks that node from the list in O(1). In Python you can skip the tree: use a max-heap with lazy deletion and a removed-ID set. Push (-value, -id), and skip stale entries when you peek. The common pitfall is removing the first max instead of the topmost one, which Example 2 tests directly. Also remember POP must clean the heap or map entry too. If the heap approach or the linked list wiring slips under pressure, StealthCoder is your hedge during the live OA.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Max Stack 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. If you're reading this with an OA window open, you're who this was built for.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as max stack. If you have time before the OA, drill that.

⏵ The honest play

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

Bloomberg reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Max Stack FAQ

What's the trick in the Bloomberg Max Stack problem?+

You need O(log n) or better for POP_MAX. Keep stack order in a doubly linked list, and track maximums separately with a heap using lazy deletion or a sorted map of value to node lists. Unlinking a node from the middle is then O(1).

How hard is Max Stack really?+

It's harder than it looks. PUSH, POP and PEEK are trivial, but POP_MAX on the topmost duplicate max forces a real design choice. With 100000 operations, brute force scanning will time out, so you need a proper structure.

Can I use a heap with lazy deletion?+

Yes. Give each pushed element a unique increasing ID, store (-value, -id) in a heap, and track removed IDs in a set. When peeking or popping the max, discard entries already removed. Do the same check when POP removes the top element.

Which duplicate does POP_MAX remove?+

The one closest to the top of the stack, meaning the most recently pushed copy of the maximum. Example 2 shows this: with 2, 2, 1, it removes the second 2, leaving 1 on top. Tie-break by latest push ID.

How do I prepare for this in 48 hours?+

Code the min stack first, then write this one from scratch using a linked list plus heap. Test both examples by hand, especially duplicate maxes and POP_MAX followed by PEEK. Also check negative values and a stack that empties and refills.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Bloomberg.

OA at Bloomberg?
Invisible during screen share
Get it