Stack,
Queue and Heap
Intermediate Level Questions:
Stack:
• Implement Stack using Queues
[Practice here: https://practice.geeksforgeeks.org/problems/stack-using-two-queues/1 ]
• How to efficiently implement “k” stacks in an array ?
[Follow here: https://www.geeksforgeeks.org/efficiently-implement-k-stacks-single-array/ ]
• Design a Stack that supports getMin() in O(1) time and O(1)
extra space.
[Follow here: https://www.geeksforgeeks.org/design-a-stack-that-supports-getmin-in-o1-time-and-
o1-extra-space/ ]
• Implement stack and Queue using deque
[Follow here: https://www.geeksforgeeks.org/implement-stack-queue-using-deque/ ]
• Implement methods for Infix to Postfix, Prefix to Infix, Prefix to
Postfix, Postfix to Infix and Postfix to prefix Conversion using
stack.
[Follow here: https://www.geeksforgeeks.org/stack-set-2-infix-to-postfix/ ]
[Follow here: https://www.geeksforgeeks.org/prefix-infix-conversion/ ]
[Follow here: https://www.geeksforgeeks.org/prefix-postfix-conversion/ ]
[Follow here: https://www.geeksforgeeks.org/postfix-prefix-conversion/ ]
[Follow here: https://www.geeksforgeeks.org/postfix-to-infix/ ]
• Find the next Greater element
[Practice here: https://practice.geeksforgeeks.org/problems/next-larger-element/0 ]
• The celebrity Problem
[Practice here: https://practice.geeksforgeeks.org/problems/the-celebrity-problem/1 ]
• Arithmetic Expression evaluation
[Practice here: https://www.geeksforgeeks.org/arithmetic-expression-evalution/ ]
• Evaluation of Postfix expression
[Practice here: https://practice.geeksforgeeks.org/problems/evaluation-of-postfix-expression/0 ]
• Implement a method to insert an element at its bottom without
using any other data structure.
• Reverse a stack using recursion
[Follow here: https://www.geeksforgeeks.org/reverse-a-stack-using-recursion/ ]
• Sort a Stack using recursion
[Practice here: https://practice.geeksforgeeks.org/problems/sort-a-stack/1 ]
• Merge Overlapping Intervals
[Practice here: https://practice.geeksforgeeks.org/problems/overlapping-intervals/0 ]
• Largest rectangular Area in Histogram
[Practice here: https://practice.geeksforgeeks.org/problems/maximum-rectangular-area-in-a-
histogram/0 ]
• Length of the Longest Valid Substring
[Practice here: https://practice.geeksforgeeks.org/problems/valid-substring/0 ]
• Expression contains redundant bracket or not
[Follow here: https://www.geeksforgeeks.org/expression-contains-redundant-bracket-not/ ]
• Find the maximum difference between nearest left and right
smaller elements
[Practice here: https://practice.geeksforgeeks.org/problems/maximum-difference/1 ]
• Remove brackets from an algebraic string containing + and –
operators
[Follow here: https://www.geeksforgeeks.org/remove-brackets-algebraic-string-containing-
operators/ ]
• Implement a Simple text Editor using Stack
[Follow here: http://algorithmsforgeeks.blogspot.com/2017/03/implement-text-editor-using-
stack.html ]
• Minimum number of bracket reversals needed to make an
expression balanced
[Practice here: https://practice.geeksforgeeks.org/problems/count-the-reversals/0 ]
Queue:
• Implement Queue using Stack
[Practice here: https://practice.geeksforgeeks.org/problems/queue-using-two-stacks/1 ]
• LRU Cache Implementation
[Practice here: https://practice.geeksforgeeks.org/problems/lru-cache/1 ]
• How to efficiently implement “k” queues in an array ?
[Follow here: https://www.geeksforgeeks.org/efficiently-implement-k-queues-single-array/ ]
• Check if a queue an be sorted into another queue using a stack
[Practice here: https://www.geeksforgeeks.org/check-queue-can-sorted-another-queue-using-stack/ ]
• Level Order Tree traversal
[Practice here: https://practice.geeksforgeeks.org/problems/level-order-traversal/1 ]
• Reverse a Queue using recursion
[Practice here: https://practice.geeksforgeeks.org/problems/queue-reversal/1 ]
• Reverse the first “K” elements of a queue
[Practice here: https://practice.geeksforgeeks.org/problems/reverse-first-k-elements-of-queue/1 ]
• Interleave the first half of the queue with second half
[Practice here: https://www.geeksforgeeks.org/interleave-first-half-queue-second-half/ ]
• Sorting a queue without extra space
[Practice here: https://www.geeksforgeeks.org/sorting-queue-without-extra-space/ ]
• Find the first circular tour that visits all Petrol Pumps
[Practice here: https://practice.geeksforgeeks.org/problems/circular-tour/1 ]
• Minimum time required to rot all oranges
[Practice here: https://practice.geeksforgeeks.org/problems/rotten-oranges/0 ]
• Find maximum level sum in Binary tree
[Practice here: https://practice.geeksforgeeks.org/problems/max-level-sum-in-binary-tree/1 ]
• Distance of nearest cell having 1 in a binary matrix
[Practice here: https://practice.geeksforgeeks.org/problems/distance-of-nearest-cell-having-1/0 ]
• First negative integer in every window of size “k”
[Practice here: https://practice.geeksforgeeks.org/problems/first-negative-integer-in-every-window-
of-size-k/0 ]
• Check if all levels of two trees are anagrams or not.
[Practice here: https://www.geeksforgeeks.org/check-if-all-levels-of-two-trees-are-anagrams-or-not/ ]
• Sum of minimum and maximum elements of all subarrays of size
“k”.
[Practice here: https://www.geeksforgeeks.org/sum-minimum-maximum-elements-subarrays-size-k/ ]
• Minimum sum of squares of character counts in a given string after
removing “k” characters.
[Practice here: https://practice.geeksforgeeks.org/problems/game-with-string/0 ]
• Queue based approach or first non-repeating character in a stream.
[Practice here: https://practice.geeksforgeeks.org/problems/first-non-repeating-character-in-a-
stream/0 ]
Heap:
• Heap Sort
[Follow here: https://www.geeksforgeeks.org/heap-sort/ ]
• “k” largest element in an array
[Practice here: https://practice.geeksforgeeks.org/problems/k-largest-elements/0 ]
• Kth smallest and largest element in an unsorted array
[Practice here: https://practice.geeksforgeeks.org/problems/kth-smallest-element/0 ]
• Check if a Binary Tree is Heap
[Practice here: https://practice.geeksforgeeks.org/problems/is-binary-tree-heap/1 ]
• Connect “n” ropes with minimum cost
[Practice here: https://practice.geeksforgeeks.org/problems/minimum-cost-of-ropes/0 ]
• Merge “K” sorted arrays.
[Practice here: https://practice.geeksforgeeks.org/problems/merge-k-sorted-arrays/1 ]
• Largest Derangement of a Sequence
[Practice here: https://www.geeksforgeeks.org/largest-derangement-sequence/ ]
• Maximum distinct elements after removing “k” elements
[Practice here: https://practice.geeksforgeeks.org/problems/maximum-distinct-elements-after-
removing-k-elements/0 ]
• Median in a stream of Running Integers
[Practice here: https://practice.geeksforgeeks.org/problems/find-median-in-a-stream/0 ]
• Largest Triplet Product in a stream
[Practice here: https://www.geeksforgeeks.org/largest-triplet-product-stream/ ]
• Convert BST to Min Heap
[Practice here: https://www.geeksforgeeks.org/convert-bst-min-heap/ ]
• Merge 2 Binary Max Heaps
[Practice here: https://practice.geeksforgeeks.org/problems/merge-two-binary-max-heap/0 ]
• Kth largest sum continuous subarrays
[Practice here: https://www.geeksforgeeks.org/k-th-largest-sum-contiguous-subarray/ ]
• Convert min heap to max heap
[Practice here: https://www.geeksforgeeks.org/convert-min-heap-to-max-heap/ ]
• Why is Binary Heap is preferred over BST for Priority Queue ?
[Answer: https://www.geeksforgeeks.org/why-is-binary-heap-preferred-over-bst-for-priority-queue/ ]
• Given Level order traversal of a Binary Tree, check if the tree is
Min heap.
[Follow here: https://www.geeksforgeeks.org/given-level-order-traversal-binary-tree-check-tree-min-
heap/ ]
• Rearrange characters in a string such that no two adjacent are
same.
[Practice here: https://practice.geeksforgeeks.org/problems/rearrange-characters/0 ]
• Minimum sum of two numbers formed from digits of an array
[Practice here: https://practice.geeksforgeeks.org/problems/min-sum-formed-by-digits/0 ]
• Leetcode- reorganize strings
[Practice here: https://leetcode.com/problems/reorganize-string/ ]
• Merge “K” Sorted Linked Lists
[Practice here: https://practice.geeksforgeeks.org/problems/merge-k-sorted-linked-lists/1 ]
• Smallest range in “K” Lists
[Practice here: https://practice.geeksforgeeks.org/problems/find-smallest-range-containing-elements-
from-k-lists/1 ]