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.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

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
doubly linked list
heap
lazy deletion
data structure design
stack
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample Answer
Requirements
  • push(x), pop() and top() 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...


Submit Your Answer
Markdown supported