TGViewer
Coding Interview Resources Coding Interview Resources @crackingthecodinginterview ยท 52.2K subscribers
Post #2963 1.24K
๐Ÿš€ Coding Interview Questions with Answers โ€” Part 4

๐Ÿ—‚๏ธ Stacks, Queues & Heaps

๐Ÿš€ 31. How do you implement a stack with a max-stack (O(1) max query)?

A Max Stack supports:
โ€ข Push
โ€ข Pop
โ€ข Get Maximum Element in O(1)

๐Ÿ”น Idea
Maintain:
1. Main stack
2. Max stack

Max stack stores current maximums.

๐Ÿ”น Python Solution

class MaxStack:
    def __init__(self):
        self.stack = []
        self.max_stack = []

    def push(self, value):
        self.stack.append(value)

        if not self.max_stack or value >= self.max_stack[-1]:
            self.max_stack.append(value)

    def pop(self):
        if self.stack[-1] == self.max_stack[-1]:
            self.max_stack.pop()

        return self.stack.pop()

    def get_max(self):
        return self.max_stack[-1]


๐Ÿ”น Complexity
Operation - Complexity
Push - O(1) 
Pop - O(1) 
Get Max - O(1) 

๐Ÿ”น Interview Tip
Very common design-based stack question.

๐Ÿš€ 32. How do you implement a queue using two stacks?

Queues are FIFO. Stacks are LIFO. 
We can combine two stacks.

๐Ÿ”น Idea
Stack1 โ†’ enqueue 
Stack2 โ†’ dequeue

๐Ÿ”น Python Solution

class Queue:
    def __init__(self):
        self.s1 = []
        self.s2 = []

    def enqueue(self, value):
        self.s1.append(value)

    def dequeue(self):
        if not self.s2:
            while self.s1:
                self.s2.append(self.s1.pop())

        return self.s2.pop()


๐Ÿ”น Complexity
Operation - Complexity
Enqueue - O(1) 
Dequeue - Amortized O(1) 

๐Ÿ”น Interview Tip
Interviewers love this because it tests understanding of stack behavior.

๐Ÿš€ 33. How do you design a stack that supports getMin() in O(1)?

Very similar to Max Stack.

๐Ÿ”น Idea
Maintain:
โ€ข Main stack
โ€ข Min stack

๐Ÿ”น Python Solution

class MinStack:
    def __init__(self):
        self.stack = []
        self.min_stack = []

    def push(self, value):
        self.stack.append(value)

        if not self.min_stack or value <= self.min_stack[-1]:
            self.min_stack.append(value)

    def pop(self):
        if self.stack[-1] == self.min_stack[-1]:
            self.min_stack.pop()

        return self.stack.pop()

    def get_min(self):
        return self.min_stack[-1]


๐Ÿ”น Complexity
Operation - Complexity
Push - O(1) 
Pop - O(1) 
Get Min - O(1) 

๐Ÿ”น Interview Tip
This is one of the highest-frequency interview problems.

๐Ÿš€ 34. What is a monotonic stack and when is it useful?

A monotonic stack maintains elements in:
โ€ข Increasing order OR
โ€ข Decreasing order

๐Ÿ”น Uses
โœ… Next Greater Element 
โœ… Largest Rectangle in Histogram 
โœ… Stock Span Problem 
โœ… Daily Temperatures 

๐Ÿ”น Example

arr = [2, 1, 3]

stack = []

for num in arr:
    while stack and stack[-1] > num:
        stack.pop()

    stack.append(num)


๐Ÿ”น Complexity
Most monotonic stack problems: 
O(n) 

because every element is pushed and popped once.

๐Ÿ”น Interview Tip
Extremely important pattern for medium/hard problems.

๐Ÿš€ 35. How do you implement a priority queue / heap?

A heap is a complete binary tree.

Types:
โ€ข Min Heap
โ€ข Max Heap

๐Ÿ”น Python Min Heap

import heapq

heap = []

heapq.heappush(heap, 10)
heapq.heappush(heap, 5)
heapq.heappush(heap, 20)

print(heapq.heappop(heap))


๐Ÿ”น Output
5

๐Ÿ”น Complexity
Operation - Complexity
Insert - O(log n) 
Delete - O(log n) 
Peek - O(1) 

๐Ÿ”น Uses
โœ… Task scheduling 
โœ… Dijkstraโ€™s algorithm 
โœ… Top K problems 
โœ… Priority processing 

๐Ÿš€ 36. How do you find the top K frequent elements?

๐Ÿ”น Approach
1. Count frequency using hashmap
2. Use heap

๐Ÿ”น Python Solution

from collections import Counter
import heapq

def top_k(nums, k):
    freq = Counter(nums)

    return heapq.nlargest(k, freq.keys(), key=freq.get)

print(top_k([1, 1, 1, 2, 2, 3], 2))


๐Ÿ”น Output
[1, 2]

๐Ÿ”น Complexity
Complexity - Value
Time - O(n log k) 
Space - O(n) 

๐Ÿ”น Interview Tip
Heap + hashmap combination is frequently tested.
More from @crackingthecodinginterview
  1. Oct 7, 2026๐Ÿš€ DSA Topics Every Programmer Should Know ๐Ÿ’ป๐Ÿ”ฅ ๐Ÿ“ฆ 1. Arrays โœ” Traversal โœ” Searching โœ” Sorโ€ฆ
  2. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  3. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  4. Sep 29, 2026โœ… Daily Coding Habits That Make You a Better Developer ๐Ÿง ๐Ÿ’ปโœจ 1๏ธโƒฃ Code Every Day (Even 30 Mโ€ฆ
  5. Sep 29, 2026๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—”๐—œ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿš€ โ€‹ Explore 6 free resourceโ€ฆ
  6. Sep 28, 2026Hereโ€™s a DSA problem-solving cheat sheet that will help you solve 90โ€“95% of questions thatโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’