Max Stack
Reported by candidates from LinkedIn's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The edge case that kills the naive Max Stack is POP_MAX with duplicate maximums. LinkedIn's OA, reported in September 2026, asks you to remove the topmost copy of the max, not just any copy and not just the top. A plain stack with a running max tracker passes PUSH, POP, PEEK and PEEK_MAX, then falls over on POP_MAX. Up to 100000 operations means an O(n) scan per POP_MAX will time out. If you blank on the structure mid-assessment, StealthCoder runs invisibly on your desktop and gives you the solution in real time as a safety net.
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 two structures that stay in sync. Keep a doubly linked list as the stack, so you can delete any node in O(1). Keep a max-ordered structure of nodes, keyed by value and then by push order, so the topmost max is always the latest-pushed node with the largest value. A heap with lazy deletion works: store (value, id), mark ids as removed when popped from the stack, and skip dead entries at the top. Pitfall: using only a value-to-count map loses position, so you can't tell which duplicate is closest to the top. Example 2 shows it. The 2 under the 1 must be removed, leaving 1 on top. Also parse each PUSH string carefully for negative values. Total cost is O(n log n). If the linked-list-plus-heap design slips away under pressure, StealthCoder is the hedge during the live OA.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
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. 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
This OA pattern shows up on LeetCode as max stack. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass LinkedIn's OA.
LinkedIn 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.
Max Stack FAQ
What's the trick in the LinkedIn Max Stack OA?+
Make POP_MAX cheap. Use a doubly linked list for the stack so any node can be deleted in O(1), plus a max-heap or sorted map ordered by value then push id. The largest id among equal maximums is the one closest to the top.
Why does a simple max-tracking stack fail here?+
The classic min/max stack stores the running max beside each element, which handles POP and PEEK_MAX. It can't remove a max buried in the middle. POP_MAX with duplicates needs deletion from the middle, so you need node-level access.
Can I use lazy deletion with a heap?+
Yes. Push (value, id) into a max-heap with ties broken by larger id. Track removed ids in a set or a flag on the node. When you peek or pop the heap, discard entries already removed. Each entry is discarded once, so the total stays O(n log n).
What edge cases should I test before submitting?+
Test duplicate maximums with a smaller value on top, as in Example 2. Test negative values and 10^9 bounds. Test POP_MAX removing the top element itself. Test a POP after POP_MAX deleted a middle node. Check the PUSH parsing with a minus sign.
How do I prepare in 48 hours for this kind of question?+
Write the linked list node class and the heap with lazy deletion from scratch once, then trace both examples by hand. Focus on the sync between structures. Two or three runs on random operation sequences against a brute-force version will catch most bugs.