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.

Be the first one to answer
Add answer anonymously...
Blinkit Software Developer Intern 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

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