Design a stack that supports getMin() in O(1) time and O(1) extra space

Implement a SpecialStack Data Structure that supports getMin() in O(1) time and O(1) extra space along with push(), pop(), top(), isEmpty(), isFull() in O(1). To implement SpecialStack, you should only use inbuilt Stack data structure.

Implement the following public functions :
1. push(data) :
This function should take one argument of type integer. It pushes the element into the stack and returns nothing.

2. pop() :
It pops the element from the top of the stack and, in turn, returns the element being popped or deleted. In case the stack is empty, it returns -1.

3. top() :
It returns the element being kept at the top of the stack. In case the stack is empty, it returns -1.

4. isEmpty() :
It returns a boolean value indicating whether the stack is empty or not.

5. getMin() :
It returns the smallest element present in the stack. In case the stack is empty, it returns -1.
Operations Performed on the Stack:
Query-1(Denoted by an integer 1): Pushes integer data to the stack. (push function)

Query-2(Denoted by an integer 2): Pops the data kept at the top of the stack and returns it to the caller. (pop function)

Query-3(Denoted by an integer 3): Fetches and returns the data being kept at the top of the stack but doesn't remove it, unlike the pop function. (top function)

Query-4(Denoted by an integer 4): Returns a boolean value denoting whether the stack is empty or not. (isEmpty function)

Query-5(Denoted by an integer 5): Returns the smallest element present in the stack. (getMin() function)
Input Format:
The first line contains an integer 'Q’, which denotes the number of queries to be run against each operation in the stack. 

The next 'Q' lines represent an operation that needs to be performed.

For the push operation, the input line will contain two integers separated by a single space, representing the type of the operation in the integer and the integer data being pushed into the stack.

For the rest of the operations on the stack, the input line will contain only one integer value, representing the query being performed on the stack.
Output Format:
For Query-1, you do not need to return anything.

For Query-2, prints the data being popped from the stack.

For Query-3, print the data kept on the top of the stack.

For Query-4, print 'TRUE' or 'FALSE'(without quotes).

For Query-5, print the smallest element present in the stack.

Output for every query will be printed in a separate line.
Note:
You are not required to print anything explicitly. It has already been taken care of. Just implement the function.
Constraints:
1 <= Q <= 1000
1 <= query type <= 5
-10^9 <= data <= 10^9 and data != -1

Where 'Q' is the total number of queries being performed on the stack and 'data' represents the integer pushed into the stack.

Time Limit: 1 sec
CodingNinjas
author
2y

1. Start from brute force approach by using another stack.
2. Optimise it with reducing push and pop operations.
3. Optimise it to O(1) space complexity.

CodingNinjas
author
2y
Optimal Solution
  • You need to make two separate stacks for solving the problem.
  • The first stack would have the actual number and the second stack would contain the minimum number present in the current s...read more
CodingNinjas
author
2y
Efficient Solution
  • You need to make two separate stacks for solving the problem.
  • The first stack would have the actual number and the second stack would contain the minimum number present in the current...read more
CodingNinjas
author
2y
Space Efficient Solution
  • We can store the minimum element encountered at any point of time in a variable, say ‘MINELE’.
  • To handle the case when the minimum element is removed, we would need to store the...read more
Add answer anonymously...
Paytm Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter