1. Introduction to Stack Interview Questions
The stack is a LIFO (Last In, First Out) data structure essential for expression parsing, nested structures, and monotonic queries.
Below are 25 essential Stack questions with complete implementations in C++, Java, Python, and JavaScript.
2. Core Stack Questions
Q1. Valid Parentheses
Question: Given a string containing only (, ), {, }, [, ], determine if the input string is valid.
cppbool isValid(string s) { stack<char> st; for (char c : s) { if (c == '(' || c == '[' || c == '{') st.push(c); else { if (st.empty()) return false; char top = st.top(); st.pop(); if (c == ')' && top != '(') return false; if (c == ']' && top != '[') return false; if (c == '}' && top != '{') return false; } } return st.empty(); }
javapublic boolean isValid(String s) { Stack<Character> st = new Stack<>(); for (char c : s.toCharArray()) { if (c == '(' || c == '[' || c == '{') st.push(c); else { if (st.isEmpty()) return false; char top = st.pop(); if (c == ')' && top != '(') return false; if (c == ']' && top != '[') return false; if (c == '}' && top != '{') return false; } } return st.isEmpty(); }
pythondef isValid(s: str) -> bool: stack = [] mapping = {')': '(', ']': '[', '}': '{'} for c in s: if c in '([{': stack.append(c) elif not stack or stack[-1] != mapping[c]: return False else: stack.pop() return len(stack) == 0
javascriptfunction isValid(s) { let stack = []; let map = { ')': '(', ']': '[', '}': '{' }; for (let c of s) { if (c === '(' || c === '[' || c === '{') { stack.push(c); } else { if (stack.length === 0 || stack[stack.length - 1] !== map[c]) return false; stack.pop(); } } return stack.length === 0; }
Time Complexity: O(n) | Space Complexity: O(n)
Q2. Min Stack
cppclass MinStack { stack<int> st, minSt; public: void push(int val) { st.push(val); minSt.push(minSt.empty() ? val : min(val, minSt.top())); } void pop() { st.pop(); minSt.pop(); } int top() { return st.top(); } int getMin() { return minSt.top(); } };
javaclass MinStack { private Stack<Integer> st = new Stack<>(); private Stack<Integer> minSt = new Stack<>(); public void push(int val) { st.push(val); minSt.push(minSt.isEmpty() ? val : Math.min(val, minSt.peek())); } public void pop() { st.pop(); minSt.pop(); } public int top() { return st.peek(); } public int getMin() { return minSt.peek(); } }
pythonclass MinStack: def __init__(self): self.stack = [] self.min_stack = [] def push(self, val: int) -> None: self.stack.append(val) min_val = val if not self.min_stack else min(val, self.min_stack[-1]) self.min_stack.append(min_val) def pop(self) -> None: self.stack.pop() self.min_stack.pop() def top(self) -> int: return self.stack[-1] def getMin(self) -> int: return self.min_stack[-1]
javascriptclass MinStack { constructor() { this.stack = []; this.minStack = []; } push(val) { this.stack.push(val); let minVal = this.minStack.length === 0 ? val : Math.min(val, this.minStack[this.minStack.length - 1]); this.minStack.push(minVal); } pop() { this.stack.pop(); this.minStack.pop(); } top() { return this.stack[this.stack.length - 1]; } getMin() { return this.minStack[this.minStack.length - 1]; } }
Time Complexity: O(1) | Space Complexity: O(n)
3. Summary Table
| Problem | Key Technique | Time | Space |
|---|---|---|---|
| Valid Parentheses | Bracket Stack | O(n) | O(n) |
| Min Stack | Auxiliary Min Stack | O(1) | O(n) |
Practice all stack problems on DSAMaster's practice platform.
