PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Coding & Algorithms/LinkedIn

Implement a stack supporting max and popMax

Last updated: Mar 29, 2026

Quick Overview

This question evaluates understanding of data structures and algorithmic complexity, focusing on designing a stack variant that supports efficient retrieval and removal of the current maximum while preserving stack behavior.

  • medium
  • LinkedIn
  • Coding & Algorithms
  • Data Engineer

Implement a stack supporting max and popMax

Company: LinkedIn

Role: Data Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Design a data structure that behaves like a stack but also supports retrieving and removing the current maximum value. Implement a class `MaxStack` with the following operations: - `push(x)`: Push integer `x` onto the stack. - `pop()`: Remove and return the top element. - `top()`: Return the top element without removing it. - `peekMax()`: Return the maximum element currently in the stack without removing it. - `popMax()`: Remove and return the maximum element currently in the stack. If there are multiple occurrences of the maximum value, remove the one closest to the top of the stack. All operations should be efficient (better than linear time when possible).

Quick Answer: This question evaluates understanding of data structures and algorithmic complexity, focusing on designing a stack variant that supports efficient retrieval and removal of the current maximum while preserving stack behavior.

Related Interview Questions

  • Count Trips From Vehicle Logs - LinkedIn (easy)
  • Design O(1) Randomized Multiset - LinkedIn (easy)
  • Process Mutable Matrix Sum Queries - LinkedIn (medium)
  • Design a Randomized Multiset - LinkedIn (medium)
  • Can You Place N Objects? - LinkedIn (medium)
LinkedIn logo
LinkedIn
Feb 2, 2026, 12:00 AM
Data Engineer
Technical Screen
Coding & Algorithms
2
0

Design a data structure that behaves like a stack but also supports retrieving and removing the current maximum value.

Implement a class MaxStack with the following operations:

  • push(x) : Push integer x onto the stack.
  • pop() : Remove and return the top element.
  • top() : Return the top element without removing it.
  • peekMax() : Return the maximum element currently in the stack without removing it.
  • popMax() : Remove and return the maximum element currently in the stack. If there are multiple occurrences of the maximum value, remove the one closest to the top of the stack.

All operations should be efficient (better than linear time when possible).

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More LinkedIn•More Data Engineer•LinkedIn Data Engineer•LinkedIn Coding & Algorithms•Data Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.