Max Stack With Fast Pop of the Largest Pending Task
Last updated: November 29, 2026
Quick Overview
Design a stack of task priorities supporting push(x), pop(), top(), peekMax() and popMax(), where popMax removes the most recently pushed copy of the maximum, with O(log n) for each operation.
0
0
0 solved
Design a stack of task priorities supporting push(x), pop(), top(), peekMax() and popMax(), where popMax removes the most recently pushed copy of the maximum, with O(log n) for each operation.
A Google onsite data structure question that cannot be solved with one container. The interviewer looks for a doubly linked list for stack order combined with a heap for max order, and a clean lazy deletion strategy linking the two.
What the Interviewer Expects
- Use a doubly linked list so any node can be removed in O(1)
- Use a max-heap of (-value, -sequence, node) to find the newest maximum
- Lazily skip heap entries whose nodes were already removed
- Keep both structures consistent on every pop
- State O(log n) amortized per operation
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Get all operations to O(1) except popMax.
- Support popMin as well.
- Explain the memory growth of lazy deletion and how to bound it.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
push(x),pop()andtop()behave like a stack.peekMax()returns the max;popMax()removes the most recent max.- O(log n) per operation....
High-Level Design
A stack alone cannot find the max quickly; a heap alone cannot remove the top of stack. Combine them. Keep stack order in a doubly linked list with sentinels, so the top is tail.prev and any node ca...