Stack and queue in Python
Stack
stack = []
stack.append(1)
stack.append(2)
stack.pop()Queue
from collections import deque
queue = deque()
queue.append(1)
queue.append(2)
queue.popleft()Problems to practice
- Valid Parentheses
- Min Stack
- Daily Temperatures
- Sliding Window Maximum
- Largest Rectangle in Histogram
- Implement Queue Using Stack
Example: valid parentheses
def is_valid(s):
stack = []
pairs = {')': '(', ']': '[', '}': '{'}
for ch in s:
if ch in '([{':
stack.append(ch)
else:
if not stack or stack.pop() != pairs[ch]:
return False
return not stackCan you explain it without the notes?
Stack and queue in Python
Use your own words. Explain an example and one limitation before checking the explanation.
Check your explanation
Stack
Turn this concept into a clear answer.
Practice this conceptAdapted from 18_DSA_PYTHON.md · “5. Stack and queue in Python”. Examples are study fragments; framework behavior can depend on your version.