Stack

When talking about Data Structures and algorithms associated with them, we need to talk about Stacks. A stack is a linear data structure, which follows a particular order in which operations can be performed. These operations may be FILO (First In Last Out) or LIFO(Last In First Out).
Stacks can be implemented using arrays and linked lists manually, while languages like C++, Java, and Python have built-in classes, STLs, and libraries to implement them.
In this category, we will look into all operations and problems related to Stacks.

Introduction

Stack is an abstract data type (ADT) that follows Last in First Out (LIFO). This means that the element which is added last in the stack will be accessed first.
Insertion of an element in the stack is termed as PUSH operation, removing of an element in the stack is termed as POP operation.
There are more operations in Stack like PEEK or TOP which will give the top element of the stack and the isEmpty() method which will return true if the stack is empty and false otherwise.
All these operations in the stack are performed in O(1) time.

Stack in C++ (STL)

By Kabir Singh

● Published At Oct 2021

Stack can be determined as a container that follows LIFO order. Through this article, we will see how to implement stack in C++ using STL. ... Keep reading ..

Stack Class in Java Collection Framework

By vaishnavi pandey

● Published At Oct 2021

Stack in Java is an important data structure. Java provides a built-in Stack class via Java Collection Framework. We’ll see to it in detail.... Keep reading ..

Stack in Python (LIFO Queue)

By Neelakshi Lahiri

● Published At Oct 2021

Stack is an important data structure that is used in all programming languages. This article describes a stack in Python, in particular. ... Keep reading ..

Python Stack Using Doubly Linked List

By dhruv sharma

● Published At Nov 2021

In this blog, we will see how to implement the stack data structure using a doubly-linked list in Python.... Keep reading ..

Difference Between Stack and Tree

By Firdausia Fatima

● Published At Jan 2022

In this blog, we'll go through the definitions of stack and tree, as well as the differences between the two.... Keep reading ..

Difference between Array, Queue and Stack

By Malay Gain

● Published At Feb 2022

In this article, we will discuss the difference between Array, Queue, and Stack.
... Keep reading ..

Implement stack using a singly linked list

By Sandeep kamila

● Published At Nov 2021

This article covers the implementation of a stack using a singly linked list with examples and its C++ code.... Keep reading ..

Implement stack using a doubly-linked list

By Sandeep kamila

● Published At Nov 2021

This article covers the implementation of a stack using a doubly-linked list with examples and its C++ code, and its time and space complexity.... Keep reading ..

Problems

Problems on stack usually involve use cases in computer science when there is a need to revisit or re-track a stored set of data. A stack data structure can be used then to traverse the data in the reverse sequence of its storage.
Various forms of problems whether in computer science such as caching, calculations, process or function call stacks, etc. or in the real world such as loading of bullets in a magazine and firing in the reverse order, unstacking a set of Russian dolls, etc. are all instances which utilize the stack-based strategy to be optimally and conveniently solved.

Next greater/smaller element using a monotonic queue

By Riya

● Published At Feb 2022

This blog covers an interview problem: find the Next greater/smaller element using a monotonic queue using various approaches along with the Java code.... Keep reading ..

Iterative Tower of Hanoi

By Soumya Agrawal

● Published At May 2022

In this article, we will discuss the iterative approach of the problem Tower of Hanoi.
... Keep reading ..

Remove Adjacent Duplicates

By Rhythm Jain

● Published At Nov 2021

This article discusses the Problem, Remove all Adjacent Duplicates, one of the most basic questions involving Stacks.... Keep reading ..

Design A Minimum Stack

By Kabir Singh

● Published At Oct 2021

This blog discusses the solution to the problem of minimum stack in which we have to return the smallest element of a stack. Read the blog to learn more!... Keep reading ..

Trapping Rainwater

By Pranav Gautam

● Published At Oct 2021

Learn various ways to find the next greater and previous greater for every array element using trapping rainwater problem. ... Keep reading ..

Delete Middle Element Of The Stack

By Shreya Deep

● Published At Jan 2022

In this article, we’ll see how to delete middle element of a stack without using any additional data structure.
... Keep reading ..

Count array elements having at least one smaller element on its left and right side

By Yukti Kumari

● Published At Nov 2021

This article explains the solution to the problem of counting array elements having at least one smaller element on its left and right side.... Keep reading ..

Minimize the length of a given string by removing subsequences forming valid parenthesis

By Shreya Deep

● Published At Nov 2021

In this article, we’ll learn how to solve an interesting problem related to deleting balanced parenthesis.... Keep reading ..

Add two numbers represented by Stacks

By Urwashi Priya

● Published At Dec 2021

This article will brief you on how to Add two numbers represented by Stacks.
... Keep reading ..

Diagonal Traverse ii

By Soumya Agrawal

● Published At Nov 2021

In this blog, we will build logic for the Diagonal traversal of integers and see the various implementations of this problem.... Keep reading ..

Minimize a binary string by repeatedly removing even-length substrings of the same characters

By Nishant Rana

● Published At Feb 2022

This blog will cover the question to minimize a binary string by repeatedly removing even length substrings of the same characters and discussing its Time and Space complexity.
... Keep reading ..

Minimize A String By Removing All Occurrences Of Another String

By Harsh Goyal

● Published At Nov 2021

This article will discuss the Minimize a string by removing all occurrences of another string problem and various ways to solve this problem, from the brute force approach to the efficient approach.
... Keep reading ..

Maximum Equal Sum in Three Stacks

By Arun Nawani

● Published At Nov 2021

In this blog, we’ll pick a problem to better Maximum Equal Sum in Three Stacks understand the stack as a data structure and its operations and how it can be used to efficiently handle certain problems.
... Keep reading ..

Print Stack Elements from Top to Bottom

By Soumya Agrawal

● Published At Nov 2021

This article will cover the common stack problem, Print Stack Elements from Top to Bottom, and its implementation. ... Keep reading ..

Smallest String Obtained By Removing All Occurrences of 01 and 11 from the Binary String

By Rhythm Jain

● Published At Nov 2021

This article discusses the Problem of obtaining the smallest string possible by removing all occurrences of 01 and 11 from a binary string.... Keep reading ..

Count of days remaining for the next day with higher temperature

By Aditya Narayan Joardar

● Published At Nov 2021

This article discusses the problem of the count of days remaining for the next day with higher temperatures.... Keep reading ..

Count of strings that do not contain Arc intersection

By Yukti Kumari

● Published At Nov 2021

This article explains the solution to the problem to find the count of strings that do not contain arc intersections.
... Keep reading ..

Minimize Length of a String by Removing Pairs of Consecutive Increasing or Decreasing Digits

By Rhythm Jain

● Published At Nov 2021

This article discusses the Problem of Minimising Length of a String by Removing Pairs of Consecutive Increasing or Decreasing Digits... Keep reading ..

Largest rectangle in histogram

By Shreya Deep

● Published At Dec 2021

In this article, we’ll learn how to find the largest rectangle in a histogram.... Keep reading ..

Reverse a linked list using Stack

By Sandeep kamila

● Published At Nov 2021

This article covers the approach of reversing a linked list using stack data structure with complete explanation and its implementation in C++.... Keep reading ..

Count subarrays for every array element in which they are the minimum

By Vibhor Bhatnagar

● Published At Nov 2021

This article will discuss the problem count subarrays for every array element in which they are the minimum.... Keep reading ..

Check if a string is a subsequence of another string using stack

By Riya

● Published At Feb 2022

This article will discuss the problem to check if a string is a subsequence of another string, its solution using stack and its C++ implementation, and the time and space complexities of the solution.... Keep reading ..

Program to insert an element at the Bottom of a Stack

By Urwashi Priya

● Published At Nov 2021

This article will brief you on how to insert an element at the Bottom of a Stack.
... Keep reading ..

Distance from Next Greater element

By Vibhor Bhatnagar

● Published At Nov 2021

In this article, we will discuss the problem distance from next greater element.
... Keep reading ..

Convert given Binary string S to all 1s by changing all 0s to 1s in range [i+1, i+K] if S[i] is ‘1’

By Aman Chourasiya

● Published At Nov 2021

In this blog, we will learn to solve a problem based on binary strings. We will discuss three approaches to solve the problem involving greedy and stack data structure concepts.... Keep reading ..

Count of groups of consecutive 1s in a given Binary String

By GAZAL ARORA

● Published At Dec 2021

In this blog, we will solve a programming question in which, in a given array, we have to count the number of groups that contain only 1s.... Keep reading ..

Check if a string consisting only of a, b, c can be made empty by removing substring “abc” recursively

By Nishant Rana

● Published At Feb 2022

This blog will cover the question to check if a string consisting only of a, b, c can be made empty by removing substring “abc” recursively and discussing its Time and Space complexity.
... Keep reading ..

Maximum range length such that A[i] is maximum in the given range for all i from [1, N]

By Vibhor Bhatnagar

● Published At Nov 2021

In this article, we will discuss the problem of Maximum range length such that A[i] is maximum in the given range for all i from [1, N].... Keep reading ..

Check if a string can be emptied by removing all subsequences of the form “10”

By Aditya Narayan Joardar

● Published At Nov 2021

This article discusses how to check if a string can be emptied by removing all the subsequences of the form "10".... Keep reading ..

Lexicographically smallest K-length subsequence from a given string

By Ishita Chawla

● Published At Dec 2021

This blog will discuss the problem to find the lexicographically smallest K-length subsequence from a given string. ... Keep reading ..

Reverse an array using Stack

By Nishant Rana

● Published At Feb 2022

This blog will cover the question to reverse an array using a Stack and discuss its Time and Space complexity.
... Keep reading ..

Count of subarrays starting or ending at an index i such that arr[i] is maximum in a subarray

By Vibhor Bhatnagar

● Published At Dec 2021

In this article, we will discuss the problem count of subarrays starting or ending at an index i such that arr[i] is maximum in a subarray.
... Keep reading ..

Check if string S1 can be formed using repeated insertions of another string S2

By Nishant Rana

● Published At Feb 2022

This blog will cover whether string S1 can be formed using repeated insertions of another string S2 and discuss its Time and Space complexity.
... Keep reading ..

Python program to reverse a stack

By Nishant Rana

● Published At Feb 2022

This blog will cover the question to reverse a Stack and discuss its Time and Space complexity.
... Keep reading ..

Modify Given Array By Reducing Each Element By Its Next Smaller Element

By Harsh Goyal

● Published At Dec 2021

This article will discuss the Modify given array by reducing each element by its next smaller element problem and various ways to solve this problem, from the brute force approach to the efficient approach.... Keep reading ..

Reverse words in a given String

By Alisha Chhabra

● Published At Nov 2021

This article revolves around the question which says Reverse words in a given String.
... Keep reading ..

Maximize the product of the subarray sum with its minimum element

By Urwashi Priya

● Published At Dec 2021

This article will brief you on how to Maximize the product of the subarray sum with its minimum element.
... Keep reading ..

Daily Temperatures

By Reet Maggo

● Published At Nov 2021

This article explains how to find the number of days remaining for the next day with higher temperatures.
... Keep reading ..

Minimum Removals to Make Valid Parentheses

By Sandeep kamila

● Published At Nov 2021

This blog will cover the approach of an interesting problem, Minimum removals to make valid parentheses with examples and C++ code.... Keep reading ..

Online Stock Span

By Urwashi Priya

● Published At Nov 2021

This article will brief you on finding the online stack span for each day of stock given as a stream of array elements.... Keep reading ..

Create Maximum Number

By Aman Chourasiya

● Published At Feb 2022

Arrays and stacks are widely asked topics in coding interviews. In this article, we will discuss a problem involving applications of topics like arrays, merge sort, and stacks.... Keep reading ..

## Top Problems related to Stack

Insert Interval

Water Droplet Mixing

Remove Consecutive Duplicates From String

Maximum in Subarrays of length K

Connect N Ropes With Minimum Cost

Reverse Stack Using Recursion

LRU Cache Implementation

Next Greater Element

Decode String

Minimum Depth Of Binary Tree

Sliding Maximum

First Negative In Every Window

Break Number

Diagonal Sum

Odd even level

Valid Parentheses

Stack using queue

Deepest Leaves Sum

Palindrome Linked List

Next Greater Element

Queue Using Stack