Min Stack Problem Statement

Design a special stack that supports the following operations in constant time:

  1. Push(num): Insert the given number into the stack.
  2. Pop: Remove and return the top element from the stack; return -1 if the stack is empty.
  3. Top: Return the top element of the stack; return -1 if the stack is empty.
  4. getMin: Return the minimum element in the stack; return -1 if the stack is empty.

Example:

Input:
1
5
1 1
1 2
4
2
3
Output:
1 2 1
Explanation:

Initially, the stack is empty.
1. Push 1: Stack becomes [1].
2. Push 2: Stack becomes [2, 1].
3. getMin: Minimum is 1.
4. Pop: Topmost is 2, stack becomes [1].
5. Top: Topmost is 1.

Constraints:

  • 1 ≤ T ≤ 1000
  • 1 ≤ N ≤ 100000
  • ΣN ≤ 200000
  • 1 ≤ X ≤ 109
AnswerBot
4mo

Design a special stack that supports constant time operations like push, pop, top, and getMin.

  • Use two stacks - one to store the elements and another to store the minimum values encountered so far

  • When ...read more

Help your peers!
Select
Add answer anonymously...

Top Software Developer Interview Questions Asked at Jio Platforms

Q. Find the given target in an array using the most optimized approach.
Q. Implement a stack data structure.
Q. Explain the rolling hash algorithm and its applications in data structures and a...read more
Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits