Design a Max Stack for Rolling Back Peak Price Changes
Last updated: November 29, 2026
Quick Overview
A pricing tool logs price edits on a stack. Design `push(x)`, `pop()`, `top()`, `peek_max()`, and `pop_max()` (removing the most recent occurrence of the maximum), with every operation in O(log n) or better.
Amazon
November 29, 20260
0
0 solved
A pricing tool logs price edits on a stack. Design `push(x)`, `pop()`, `top()`, `peek_max()`, and `pop_max()` (removing the most recent occurrence of the maximum), with every operation in O(log n) or better.
A hard onsite design-a-structure question in Amazon's style, framed around undoing the most aggressive price change in a seller tool. Interviewers want two coordinated structures with lazy deletion, correct tie-breaking toward the most recent push, and an honest complexity analysis.
What the Interviewer Expects
- Explains why a single auxiliary max stack fails for pop_max
- Pairs a stack with a max-heap keyed by (value, insertion id)
- Uses a removed-id set for lazy deletion in both structures
- Breaks ties by most recent push
- Analyzes amortized O(log n) 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
- Can you get O(log n) worst case using a doubly linked list plus a sorted map?
- How would you bound memory when many lazy-deleted entries pile up?
- How would you add pop_min as well?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Standard stack operations plus peek_max and pop_max. If the maximum appears several times, pop_max removes the one pushed most recently (the one closest to the top). Calls on an empty stack are ...
High-Level Design
The classic min-stack trick (a parallel stack of running maxima) handles peek_max but not pop_max, because removing an element from the middle invalidates every running max above it. Instead keep ...