Reverse the first “K” elements of a queue

Problem Statement: You are given a queue and an element k, you need to reverse the first ‘k’ elements in the queue. Example Input: Q = [1, 2, 3, 4, 5] k = 3 Output: Q = [3, 2, 1, 4, 5] Solution We need to use additional stack to solve this problem. Take a […]

Queue Reversal

Problem Statement: Given a queue, you need to reverse it. Solution This problem can be solved in 2 different methods. Method 1: Using stack. Pop the elements from queue and insert into stack. Then pop the elements from stack and insert into queue. Method 2: Using recursion. In recursion, we pop an element and store […]

Stack: Check if a stack is a permutation of another stack

Problem Statement: You are given 2 array, check one array is permutation of another array. Example Input : First array: 1, 2, 3 Second array: 2, 1, 3 Output : Yes Solution In this solution we will convert the input queue to the output queue using stack and check. Take 2 queue, and insert both […]

Implement Stack using Queues

Problem Statement: You need to create stack data structure using only queue data structure. Solution As we know that for stack insertion and deletion happen on the same end. i.e last in first out. But in queue, insertion and deletion will happen on different end. First in first out. Steps to solve this problem: While […]

Stack: Expression contains redundant bracket or not

Problem Statement: You are given an expression in the form of a string. You need to check if the string has redundant brackets. Example Input: ((a+b)) Output: True Solution We will use stack to solve this problem. If the current character is not a closing parenthesis “)” then push the char into the stack. If […]

Stack: Length of the longest valid substring

Problem Statement: You are given a string, you need to find the length of longest valid parenthesis. Example Input : ((() Output : 2 Solution For the solution, we will use one stack and follow below steps: 1. Initialize stack with -1. 2. When we encounter “(” then we push the index of “(” into […]

Largest Rectangular Area in a Histogram

Problem Statement: You are given an array that represents a bar in a histogram. You need to find out the largest rectangle that can be made from number of continuous bars. Example Input = {6, 2, 5, 4, 5, 1, 6} Output = 12. Solution We shall solve this problem by using stacks. When we […]

Sort a stack

Problem Statement: You are given a stack, you need to sort it. Solution This problem can be solved in 2 different methods: 1. Sorting using another stack 2. Sorting using recursion 1. Sorting using another stack 1. Create a temp stack. 2. Till the input stack is not empty repeat below steps: 3. Pop the […]

Stack: Reverse a stack using recursion

Problem Statement: You are given a stack, you need to reverse the stack without using while, for. You need to reverse using recursion. Solution The solution is very simple. We will use 2 functions reverse and insert at bottom. The reverse function will create a recursion to reverse the stack. We pop the element from […]

Insert at the end of stack

Problem Statement: You are given an element, you need to insert it at the end of the stach without using any data structure. Solution Solution is very simple. Here we need to use recursion. Untill we reach at the end of the stack, we will take a temp variable to pop the element. Then we […]