Implement Stack Using Two Queues — Two Approaches Explained
Implement a stack using two queues with costly push and costly pop approaches. Complete Python solutions with complexity analysis.
36 posts · page 1 of 1
Implement a stack using two queues with costly push and costly pop approaches. Complete Python solutions with complexity analysis.
Solve the 132 Pattern problem using a monotonic stack scanning right to left. Python solution tracking s3 candidates and s2 maximum, with detailed trace.
Solve the asteroid collision problem (LeetCode 735) using a stack. Covers collision rules, Python implementation, and all edge cases.
Solve Basic Calculator problems LeetCode 224, 227, and 772. Master stack-based expression evaluation with +, -, *, /, and parentheses in Python.
Solve LeetCode 853 Car Fleet using a stack. Sort by position, compare arrival times, and count fleets. Python solution with visual trace.
Solve the celebrity problem in O(n) time using a stack elimination technique. Includes proof of correctness, Python code, and matrix examples.
Decode nested encoded strings like '3[a2[c]]' using a stack. Complete walkthrough with Python solution, traces, and edge cases.
Solve LeetCode 150 Evaluate Reverse Polish Notation using a stack. Python implementation with division gotcha, traces, and complexity analysis.
Solve Exclusive Time of Functions with a stack simulating a call stack. Python solution with timestamps, detailed trace, and edge case handling.
Implement a queue using two stacks with amortized O(1) operations. Covers costly enqueue vs costly dequeue approaches with Python code.
Design a Flatten Nested List Iterator using a stack for lazy flattening. Python solution with iterator protocol, step-by-step trace, and design analysis.
Find the largest rectangle in a histogram using a monotonic stack in O(n). Detailed walkthrough, Python code, visual trace, and common pitfalls.
Solve LeetCode 1249 Minimum Remove to Make Valid Parentheses using a stack. Two-pass and one-pass approaches with Python code and traces.
Find the maximal rectangle containing only 1s in a binary matrix. Builds on the largest rectangle in histogram technique with detailed explanation.
Maximum Frequency Stack solved with HashMap and stack groups by frequency. Python implementation with step-by-step trace, complexity analysis, and design insights.
Next Greater Element II solved with monotonic stack and circular array double-length trick. Python solution with step-by-step trace, complexity analysis, and patterns.
Design an Online Stock Span class (LeetCode 901) using a stack. Covers amortized analysis, Python implementation, and the price-span pair technique.
Solve the Next Smaller Element problem with a monotonic increasing stack in O(n). Python code, step-by-step trace, and reusable pattern for interviews.
Comprehensive catalog of 15+ stack and queue interview patterns with when-to-use guide, Python templates, complexity analysis, and problem mapping for coding interviews.
Solve LeetCode 316 Remove Duplicate Letters using monotonic stack with frequency and visited tracking. Smallest lexicographic subsequence in Python.
Learn to detect redundant parentheses in expressions using a stack. Covers the algorithm, Python implementation, and edge cases with traces.
Real-world applications of stacks and queues: undo/redo systems, browser history, call stacks, task scheduling, message queues, and BFS web crawlers with Python examples.
Use a monotonic stack to remove k digits from a number to make it as small as possible. LeetCode 402 solution with Python code and traces.
Solve Simplify Path LeetCode 71 with a stack. Handle ., .., multiple slashes, and edge cases. Python solution with step-by-step trace.
Solve Score of Parentheses using a stack to track nested scores. Python solution with trace, O(n) time, plus the bit-shift trick for O(1) space.
Learn how to sort a stack using only one additional stack. Step-by-step trace, Python implementation, and O(n²) complexity analysis.
Tag Validator solved with stack-based HTML/XML tag matching and CDATA parsing. Python solution with edge cases, step-by-step trace, and complexity analysis.
Solve the stock span problem efficiently using a monotonic stack. Includes brute force vs optimal approach, Python code, and visual trace.
Solve Sum of Subarray Minimums using the contribution technique with monotonic stacks. Python solution with modular arithmetic, traces, and O(n) analysis.
Solve Trapping Rain Water (LeetCode 42) using a stack-based approach. Python code with detailed trace, comparison with two-pointer, and complexity analysis.
Validate Stack Sequences solved by simulating push and pop operations. Python solution with step-by-step trace, edge cases, and complexity analysis for LeetCode 946.
Master stack-based parsing patterns — balanced parentheses, minimum removals, longest valid parentheses, decode string, and basic calculator with Python solutions.
Build a calculator from scratch — learn infix, prefix, and postfix notation, the Shunting Yard algorithm, postfix evaluation, and Python implementation.
Master the monotonic stack pattern — solve Next Greater Element, Next Smaller, stock span, and circular array variants with Python templates and visual walkthroughs.
A practical introduction to stacks and queues — LIFO vs FIFO, using a Python list as a stack, collections.deque as a queue, and the real-world problems each one solves cleanly.
Eight classic stack and queue interview problems with worked Python solutions — Valid Parentheses, Min Stack, Daily Temperatures, Sliding Window Maximum, and more.