Max Stack Supporting Undo of the Largest Profile Edit

Last updated: November 29, 2026

Quick Overview

Implement a stack of edit records with push, pop, top, peekMax, and popMax, where popMax removes the most recently pushed among the maximum elements. Target O(log n) for every operation.

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

0

0

0 solved


Implement a stack of edit records with push, pop, top, peekMax, and popMax, where popMax removes the most recently pushed among the maximum elements. Target O(log n) for every operation.

A data structure design problem with a long history in LinkedIn onsites. The interviewer expects an O(n) popMax version first, then a two-structure design with lazy deletion that brings everything to O(log n).

What the Interviewer Expects
  • Starts with a simple version and identifies popMax as the bottleneck
  • Pairs a stack with a max-heap keyed by (value, insertion id)
  • Uses lazy deletion with a removed-id set to keep both structures consistent
  • Breaks ties by most recent insertion for popMax
  • Proves amortized O(log n) per operation
Key Topics to Cover
data structure design
heap
lazy deletion
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
  • How would you implement this with a doubly linked list and a sorted map instead?
  • What happens to memory if many items are lazily deleted and never cleaned?
  • How would you make it thread-safe?
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

Elements are comparable values (say edit sizes). pop removes the top. popMax removes the largest value; if several share it, remove the one closest to the top. top and peekMax do not remove. C...

High-Level Design

Give every push a unique, increasing id. Keep a list stack of (id, value) and a heap of (-value, -id). The heap top is the max value with the newest id. Removals from either structure only mark ...


Submit Your Answer
Markdown supported