Stack with getMin Operation

Create a stack data structure that supports not only the usual push and pop operations but also getMin(), which retrieves the minimum element, all in O(1) time complexity without using extra space for storing additional stack data structures.

The stack should have methods: push(), pop(), top(), isEmpty(), and getMin() with all operations working in constant time.

Input:

An integer 'Q' indicating the number of queries, followed by 'Q' lines of queries.
- For push operation: Two space-separated integers denoting a 1 (as the operation type) and the integer to push onto the stack.
- For other operations: A single integer specifying the operation type (2 for pop, 3 for top, 4 for isEmpty, 5 for getMin).

Output:

Output for each query in a separate line, depending on the operation type:
- For pop (type 2), print the popped element.
- For top (type 3), print the element at the top of the stack.
- For isEmpty (type 4), print 'TRUE' or 'FALSE'.
- For getMin (type 5), print the smallest element in the stack.
No output is required for push operations.

Example:

Input:
6
1 5
1 3
5
3
2
4
Output:
3
3
5
FALSE

Constraints:

  • 1 ≤ Q ≤ 1000
  • 1 ≤ query type ≤ 5
  • -109 ≤ data ≤ 109 and data ≠ -1

Note:

Focus on implementing the function as the I/O operations are already managed.

AnswerBot
4mo

Implement a stack with getMin operation in O(1) time complexity without using extra space.

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

  • W...read more

Help your peers!
Select
Add answer anonymously...

Blinkit Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Top View of Binary Tree Given a binary tree of integers, the task is to return t...read more
A Software Developer Intern was asked Q. Maximum Possible Time Problem Statement Given an array ARR consisting of exactly...read more
A Software Developer Intern was asked Q. Sum of LCM Problem Statement Given an integer 'N', calculate and print the sum o...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Maximum Possible Time Problem Statement Given an array ARR consisting of exactly...read more
A Software Developer Intern was asked Q2. Sum of LCM Problem Statement Given an integer 'N', calculate and print the sum o...read more
A Software Developer Intern was asked Q3. Vertical Order Traversal Problem Statement Given a binary tree, return the verti...read more
Blinkit Software Developer Intern 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