DEV Community

Harsh Mishra
Harsh Mishra

Posted on

Stacks: 50 Leetcode Questions

Top 50 Must-Do LeetCode Questions on Stacks for Your Coding Interview Preparation

In this article, I have compiled a list of the top 50 LeetCode questions focused on Stacks, a fundamental data structure that you’ll encounter frequently in coding interviews. These problems are categorized into Easy, Medium, and Hard difficulty levels, ensuring comprehensive coverage of the topic.


Easy Questions (20 Questions)

  1. Valid Parentheses (20)

    Check if a string of brackets is valid, meaning every open bracket has a corresponding close bracket in the correct order.

  2. Min Stack (155)

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

  3. Implement Stack using Queues (225)

    Use one or two queues to implement a stack with standard stack operations.

  4. Baseball Game (682)

    Simulate a baseball game where valid operations include adding scores or removing the last added score.

  5. Next Greater Element I (496)

    Find the next greater element for each number in an array by leveraging a stack for efficiency.

  6. Remove All Adjacent Duplicates in String (1047)

    Remove all adjacent duplicate letters from a string until no more adjacent duplicates exist.

  7. Implement Queue using Stacks (232)

    Use two stacks to implement a queue with standard queue operations.

  8. Daily Temperatures (739)

    For each day, find out how many days you’d have to wait for a warmer temperature.

  9. Final Prices With a Special Discount (1475)

    Calculate final prices of items considering discounts based on their next smaller element.

  10. Backspace String Compare (844)

    Compare two strings considering backspace (#) characters and their effects.

  11. Check If Word Is Valid After Substitutions (1003)

    Validate if a string can be reduced to an empty string by repeatedly removing "abc".

  12. Remove Outermost Parentheses (1021)

    Remove the outermost parentheses of a valid parentheses string.

  13. Sort Array by Increasing Frequency (1636)

    Sort an array by the frequency of elements and use stacks for auxiliary operations.

  14. Next Greater Node In Linked List (1019)

    Find the next greater node value for every node in a linked list using stacks.

  15. Reverse Substrings Between Each Pair of Parentheses (1190)

    Reverse substrings enclosed in parentheses in a string.

  16. Decode String (394)

    Decode a string encoded with patterns like "3[a2[c]]" using a stack.

  17. Longest Valid Parentheses (32)

    Find the length of the longest valid parentheses substring.

  18. Evaluate Reverse Polish Notation (150)

    Evaluate an expression in Reverse Polish Notation (RPN) using a stack.

  19. Find the Winner of the Circular Game (1823)

    Simulate a circular game using stacks to eliminate players and find the winner.

  20. Check If a Parentheses String Can Be Valid (2116)

    Determine if a string with * and () can be a valid parentheses sequence.


Medium Questions (25 Questions)

  1. Next Greater Element II (503)

    Find the next greater element for each number in a circular array.

  2. Score of Parentheses (856)

    Compute the score of a balanced parentheses string using stacks.

  3. 132 Pattern (456)

    Check if there is a 132 pattern in an array using a stack.

  4. Car Fleet (853)

    Determine the number of car fleets that arrive at the destination.

  5. Asteroid Collision (735)

    Simulate the collision of asteroids moving in opposite directions.

  6. Exclusive Time of Functions (636)

    Calculate the exclusive time of each function from a sequence of logs.

  7. Remove K Digits (402)

    Remove k digits from a number to form the smallest number possible.

  8. Maximum Frequency Stack (895)

    Design a stack that returns the most frequently added element.

  9. Simplify Path (71)

    Simplify an absolute file path in Unix-like file systems.

  10. Online Stock Span (901)

    Calculate the stock span for a given stock's price on a series of days.

  11. Minimum Remove to Make Valid Parentheses (1249)

    Remove the minimum number of invalid parentheses to make the string valid.

  12. Largest Rectangle in Histogram (84)

    Find the largest rectangle area in a histogram.

  13. Basic Calculator II (227)

    Evaluate a mathematical expression string with +, -, *, /.

  14. Trapping Rain Water (42)

    Calculate how much water can be trapped between bars of varying heights.

  15. Find the Most Competitive Subsequence (1673)

    Find the smallest subsequence of k elements from the array in the same order.

  16. Maximum Width Ramp (962)

    Find the maximum width ramp between indices such that values decrease.

  17. Validate Stack Sequences (946)

    Check if a sequence of pushed and popped integers is valid.

  18. Expression Add Operators (282)

    Add operators (+, -, *) between digits to evaluate to a target.

  19. Design Browser History (1472)

    Simulate browser navigation using stacks for back and forward actions.

  20. Sum of Subarray Minimums (907)

    Calculate the sum of the minimum values of all subarrays.

  21. Basic Calculator (224)

    Evaluate a basic mathematical expression string with +, -, and parentheses.

  22. Remove Duplicate Letters (316)

    Remove duplicate letters to form the smallest lexicographical string.

  23. The Earliest Moment When Everyone Become Friends (1101)

    Use stacks to determine the earliest time a group becomes fully connected.

  24. Clone a Stack Without Affecting the Original

    Clone a stack recursively or iteratively without altering its contents.

  25. Redundant Connection (684)

    Find the redundant edge that, when removed, leaves a tree.


Hard Questions (5 Questions)

  1. Expression Add Operators (282)

    Form expressions with operators (+, -, *) to evaluate to a target number.

  2. Basic Calculator III (772)

    Evaluate a mathematical expression string with +, -, *, /, and parentheses.

  3. Maximum Rectangle (85)

    Find the maximal rectangle of 1s in a binary matrix.

  4. Minimize Deviation in Array (1675)

    Minimize the maximum difference between any two elements in the array.

  5. Remove Invalid Parentheses (301)

    Remove the minimum number of invalid parentheses to make all strings valid.


By practicing these questions, you will build a strong foundation in Stacks and enhance your problem-solving skills for technical interviews. Which one are you starting with today?

Top comments (0)