← LinkedIn Interview Insights

LinkedIn·Software Engineer·Onsite - Coding / Algorithms·Intermediate

IntermediatePrefer not to say
May 2026

Summary

LinkedIn SWE coding round, one question the whole time and it was a data structure design problem that looked deceptively straightforward until the popMax part showed up.

Questions Asked (1)

Q1

Design a stack that supports push, pop, top, peekMax, and popMax operations, where popMax removes the maximum value closest to the top if there are duplicates. Implement it and analyze the time complexity of each operation.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The push/pop/top part is trivial and I kind of rushed through it, which in hindsight was dumb because I should've used that time to think about the max operations.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the requirements and edge cases, especially the duplicate handling for popMax. Then propose a solution using two stacks (one for values, one for maximums) and implement it, analyzing time complexities. Discuss trade-offs and possible optimizations.

Pro tip: Mention that the two-stack approach gives O(1) for push, pop, top, and peekMax, but popMax is O(n) in the worst case; however, you can optimize popMax to O(log n) using a balanced BST or a heap with lazy deletion, but that adds complexity. Showing awareness of these trade-offs demonstrates maturity.

1. Clarify requirements and edge cases

Ask about duplicate handling, empty stack behavior, and whether popMax should remove the topmost maximum. Confirm that operations should be efficient.

2. Propose a data structure

Suggest using two stacks: one to store all elements, and another to keep track of the maximum values. Explain how they work together.

3. Implement the operations

Write code for push, pop, top, peekMax, and popMax. For popMax, temporarily pop elements until the max is found, then push back the remaining elements.

4. Analyze time complexity

State that push, pop, top, and peekMax are O(1), while popMax is O(n) in the worst case due to the need to pop and push back elements.

5. Discuss optimizations and trade-offs

Mention alternative approaches like using a balanced BST or heap with lazy deletion to achieve O(log n) for popMax, but note the added complexity and space overhead.

Key Points to Mention

  • Two-stack approach for O(1) push, pop, top, and peekMax.
  • popMax requires O(n) time in the worst case because it may need to pop many elements.
  • Handling duplicates: popMax removes the maximum value closest to the top.
  • Edge cases: empty stack, popping from empty stack, and when all elements are the same.
  • Trade-offs: simplicity vs. performance; alternative data structures like balanced BST or heap with lazy deletion.
  • Space complexity: O(n) for the two stacks.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.