
- Main
- Catalog
- Computer science
- Coding Interview
Coding Interview
Channel has quality stuff and active English speaking audience
Channel statistics
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.
🚀 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
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
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
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
class Node:
def __init__(self, data):
self.data = data
self.next = None
def reverse(head):
prev = None
current = head
while current:
next_node = current.next
current.next = prev
prev = current
current = next_node
return prev
{}
🔹 Complexity
Time → O(n)
Space → O(1)
🔹 Interview Tip
This is one of the most important linked-list questions.
🚀 22. How do you detect a cycle in a linked list?
Use Floyd’s Cycle Detection Algorithm.
Also called: Tortoise and Hare Algorithm
🔹 Idea
• Slow pointer moves 1 step
• Fast pointer moves 2 steps
• If they meet → cycle exists
🔹 Python Solution
def has_cycle(head):
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False
{}
🔹 Complexity
Time → O(n)
Space → O(1)
🔹 Interview Tip
Very common interview question.
🚀 23. How do you find the middle node of a linked list?
Use two pointers.
🔹 Approach
• Slow pointer → moves 1 step
• Fast pointer → moves 2 steps
When fast reaches end:
slow = middle
🔹 Python Solution
def middle_node(head):
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
{}
🔹 Complexity
Time → O(n)
Space → O(1)
🔹 Interview Tip
Two-pointer technique is heavily used in linked lists.
🚀 24. How do you merge two sorted linked lists?
🔹 Example
1 → 3 → 5
2 → 4 → 6
Merged:
1 → 2 → 3 → 4 → 5 → 6
🔹 Python Solution
def merge_lists(l1, l2):
dummy = Node(0)
current = dummy
while l1 and l2:
if l1.data < l2.data:
current.next = l1
l1 = l1.next
else:
current.next = l2
l2 = l2.next
current = current.next
current.next = l1 or l2
return dummy.next
{}
🔹 Complexity
Time → O(n + m)
Space → O(1)
🔹 Interview Tip
This problem is the base concept behind merge sort on linked lists.
🚀 25. How do you find and remove a duplicate in a list?
🔹 Using HashSet
def remove_duplicates(head):
seen = set()
current = head
prev = None
while current:
if current.data in seen:
prev.next = current.next
else:
seen.add(current.data)
prev = current
current = current.next
return head
{}
🔹 Complexity
Time → O(n)
Space → O(n)
🔹 Without Extra Space
Can also be solved using nested loops: O(n²)
🔹 Interview Tip
Interviewers may ask: Can you solve it without extra memory?
🚀 26. How do you implement a dummy head in linked-list problems?
A dummy node simplifies edge cases.
🔹 Why Useful?
Without dummy node: Handling head insertion/deletion becomes complex
With dummy node: Logic becomes cleaner
🔹 Example
dummy = Node(0)
dummy.next = head
{}
🔹 Use Cases
✅ Remove nodes
✅ Merge lists
✅ Partition lists
✅ Reverse sublists
🔹 Interview Tip
Using dummy nodes often makes solutions cleaner and bug-free.
🚀 27. How do you delete a node given only that node (no head)?
Important constraint: No access to head pointer
🔹 Trick
Copy next node value into current node.
🔹 Python Solution
def delete_node(node):
node.data = node.next.data
node.next = node.next.next
{}
🔹 Limitation
Cannot delete last node because no next node exists.
🔹 Interview Tip
Classic interview trick question.
🚀 28. How do you implement a circular linked list?
In a circular linked list: Last node → points to head instead of NULL.
🔹 Visualization
1 → 2 → 3
↑ ↓
← ← ← ←
🔹 Python Example
class Node:
def init(self, data):
self.data = data
self.next = NoneReviews channel
14 total reviews
- Added: Newest first
- Added: Oldest first
- Rating: High to low
- Rating: Low to high
Catalog of Telegram Channels for Native Placements
Coding Interview is a Telegram channel in the category «Интернет технологии», offering effective formats for placing advertising posts on TG. The channel has 52.0K subscribers and provides quality content. The advertising posts on the channel help brands attract audience attention and increase reach. The channel's rating is 12.5, with 14 reviews and an average score of 5.0.
You can launch an advertising campaign through the Telega.in service, choosing a convenient format for placement. The Platform provides transparent cooperation conditions and offers detailed analytics. The placement cost is 30.0 ₽, and with 86 completed requests, the channel has established itself as a reliable partner for advertising on Telegram. Place integrations today and attract new clients!
You will be able to add channels from the catalog to the cart again.
Комментарий