PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates data-structure design and algorithmic complexity analysis by requiring a stack-like structure that supports standard stack operations plus efficient maximum retrieval and targeted removal, assessing practical implementation skills and time-complexity reasoning at a practical application level.

  • medium
  • LinkedIn
  • Coding & Algorithms
  • Software Engineer

Design a stack with max removal

Company: LinkedIn

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Design a stack-like data structure for integers that supports the following operations efficiently: - `push(x)`: insert `x` onto the stack. - `pop()`: remove and return the top element. - `top()`: return the top element without removing it. - `peekMax()`: return the maximum value currently in the stack. - `popMax()`: remove and return the maximum value currently in the stack. If the maximum value appears multiple times, remove the one closest to the top. Implement the data structure and analyze the time complexity of each operation.

Quick Answer: This question evaluates data-structure design and algorithmic complexity analysis by requiring a stack-like structure that supports standard stack operations plus efficient maximum retrieval and targeted removal, assessing practical implementation skills and time-complexity reasoning at a practical application level.

Simulate push, pop, top, peekMax, and popMax. popMax removes the maximum closest to the top.

Constraints

  • Inputs are Python literals matching the function signature.
  • Return a deterministic exact-match value.

Examples

Input: ([['push',5],['push',1],['push',5],['top'],['popMax'],['top'],['peekMax'],['pop']],)

Expected Output: [None, None, None, 5, 5, 1, 5, 1]

Explanation: Classic max stack behavior.

Input: ([['pop'],['push',2],['peekMax']],)

Expected Output: [None, None, 2]

Explanation: Empty pop.

Hints

  1. Model object-style prompts as operation streams when needed.
  2. Handle empty and boundary cases before the main logic.
Last updated: Jun 27, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ 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
  • AI Coding 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.

Related Coding 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)