TGViewer
Coding Interview Resources Coding Interview Resources @crackingthecodinginterview ยท 52.2K subscribers
Post #2964 1.8K
๐Ÿš€ 37. How do you merge K sorted lists?

๐Ÿ”น Efficient Approach
Use a Min Heap.

Heap stores: 
smallest current node

๐Ÿ”น Python Idea

import heapq
heapq.heappush(heap, (node.val, node))


Repeatedly:
โ€ข Pop smallest node
โ€ข Add next node from same list

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

Where: 
n = total nodes 
k = number of lists 

๐Ÿ”น Interview Tip 
Very common hard interview problem.

๐Ÿš€ 38. How do you implement LRU / LFU cache?

๐Ÿ”น LRU Cache 
LRU: Least Recently Used 
Remove least recently accessed item.

๐Ÿ”น Efficient Design 
Use: 
1. HashMap
2. Doubly Linked List

๐Ÿ”น Python LRU Example

python
from collections import OrderedDict

class LRUCache:
    def init(self, capacity):
        self.cache = OrderedDict()
        self.capacity = capacity

    def get(self, key):
        if key not in self.cache:
            return -1

        self.cache.move_to_end(key)

        return self.cache[key]

    def put(self, key, value):
        if key in self.cache:
            self.cache.move_to_end(key)

        self.cache[key] = value

        if len(self.cache) > self.capacity:
            self.cache.popitem(last=False)

๐Ÿ”น Complexity 
Operation - Complexity 
Get - O(1) 
Put - O(1) 

๐Ÿ”น Interview Tip 
LRU cache is a FAANG-favorite system design question.

๐Ÿš€ 39. How do you check for balanced parentheses?

Use a stack.

๐Ÿ”น Idea 
โ€ข Push opening brackets.
โ€ข When closing bracket appears: Check top of stack

๐Ÿ”น Python Solution

python
def is_valid(s):
    stack = []

    mapping = {
        ')': '(',
        '}': '{',
        ']': '['
    }

    for char in s:
        if char in mapping.values():
            stack.append(char)

        elif char in mapping:
            if not stack or stack.pop() != mapping[char]:
                return False

    return not stack

print(is_valid("({[]})"))

๐Ÿ”น Output 
True 

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

๐Ÿ”น Uses 
โœ… Compilers 
โœ… Expression parsing 
โœ… Syntax validation 

๐Ÿš€ 40. How do you implement a circular queue?

Circular queue reuses empty spaces efficiently.

๐Ÿ”น Visualization 
Front โ†’ [1,2,3,_,_] 

After dequeue + enqueue: 
[,2,3,4,] 

๐Ÿ”น Python Implementation`

python
class CircularQueue:
    def init(self, size):
        self.queue = [None] * size
        self.front = 0
        self.rear = 0
        self.size = size
        self.count = 0

    def enqueue(self, value):
        if self.count == self.size:
            return "Full"

        self.queue[self.rear] = value
        self.rear = (self.rear + 1) % self.size
        self.count += 1

    def dequeue(self):
        if self.count == 0:
            return "Empty"

        value = self.queue[self.front]
        self.front = (self.front + 1) % self.size
        self.count -= 1

        return value
`


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

๐Ÿ”น Real-World Uses 
โœ… CPU scheduling 
โœ… Streaming systems 
โœ… Buffers 
โœ… Embedded systems 

๐Ÿ”ฅ Double Tap โค๏ธ For Part-5
  • โค 3
  • ๐Ÿ† 1
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 โ†’