All content related to stack

Sort a stack using Stack

Given a stack, write a program to sort the stack in ascending order. We are not allowed to make any assumptions about how the stack is implemented. The only functions to be used are push(s, x), pop(s), top(s), isEmpty(s).

EnjoyAlgorithms

Implement Stack using Queues

Write a program to implement a stack using the queues. The implemented stack should support standard operations like push(x), pop(), and top(). This is an excellent problem to learn problem-solving and visualize the use case of stack and queue operations.

EnjoyAlgorithms

Implement Queue using Stacks

Write a program to Implement a queue using the stack. The implemented queue should support standard operations like enqueue, dequeue, and front. This is an excellent problem to learn problem-solving and visualize the use case of stack and queue operations.

EnjoyAlgorithms

Find next greater elements in an array

Given an array, find the next greater element for every element in the array. The next greater element for an element is the first greater element on the right side of the array. This is one of the best problems to learn problem-solving using stack.

EnjoyAlgorithms

Iterative Binary Tree Traversal using Stack: Preorder, Inorder and Postorder

In recursive DFS traversals of a binary tree, we have three basic elements to traverse— root, left subtree, and right subtree. The traversal order depends on the order in which we process the root node. Here recursive code is simple and easy to visualize — only one function parameter and 3–4 lines of code. So critical question would be — How can we convert it into iterative code using stack? To simulate the recursive traversal into an iterative traversal, we need to understand the flow of recursive calls.

EnjoyAlgorithms

Trapping Rain Water

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining. This is a famous rainwater trapping problem.

EnjoyAlgorithms

Our Weekly Newsletter

Subscribe to get free weekly content on data structure and algorithms, machine learning, system design, oops and math.