Coding challenge examples for senior Python developers with solutions
Here are some advanced coding challenge examples for senior Python developers, including solutions to illustrate key concepts and problem-solving approaches:
PYTHON DEVELOPER
1. Merge K Sorted Linked Lists
Merge multiple sorted linked lists into one sorted list efficiently using a min-heap.
python
import heapq class ListNode: def init(self, val=0, next=None): self.val = val self.next = next def merge_k_lists(lists): heap = [] for i, node in enumerate(lists): if node: heapq.heappush(heap, (node.val, i, node)) head = tail = ListNode(None) while heap: val, i, node = heapq.heappop(heap) tail.next = node tail = tail.next if node.next: heapq.heappush(heap, (node.next.val, i, node.next)) return head.next
2. Trapping Rain Water
Find how much water can be trapped after raining over various heights — classic two-pointer technique.
python
def trap(height): if not height: return 0 left, right = 0, len(height) - 1 left_max, right_max = height[left], height[right] water = 0 while left < right: if left_max < right_max: left += 1 left_max = max(left_max, height[left]) water += left_max - height[left] else: right -= 1 right_max = max(right_max, height[right]) water += right_max - height[right] return water
3. Word Break (Dynamic Programming)
Determine if a string can be segmented into dictionary words.
python
def word_break(s, word_dict): word_set = set(word_dict) dp = [False] * (len(s) + 1) dp[0] = True for i in range(1, len(s) + 1): for j in range(i): if dp[j] and s[j:i] in word_set: dp[i] = True break return dp[len(s)]
4. LRU Cache Implementation
Design an LRU cache with O(1) access time using collections.OrderedDict.
python
from collections import OrderedDict class LRUCache(OrderedDict): def init(self, capacity: int): self.capacity = capacity super().__init__() def get(self, key: int) -> int: if key not in self: return -1 self.move_to_end(key) return self[key] def put(self, key: int, value: int) -> None: if key in self: self.move_to_end(key) self[key] = value if len(self) > self.capacity: self.popitem(last=False)
5. Find Median from Data Stream
Maintain the median in a stream of numbers using two heaps.
python
import heapq class MedianFinder: def init(self): self.small = [] # max heap (invert values) self.large = [] # min heap def addNum(self, num): heapq.heappush(self.small, -num) if (self.small and self.large and (-self.small[0]) > self.large[0]): val = -heapq.heappop(self.small) heapq.heappush(self.large, val) if len(self.small) > len(self.large) + 1: val = -heapq.heappop(self.small) heapq.heappush(self.large, val) if len(self.large) > len(self.small): val = heapq.heappop(self.large) heapq.heappush(self.small, -val) def findMedian(self): if len(self.small) > len(self.large): return -self.small[0] return (-self.small[0] + self.large[0]) / 2
6. Number of Islands (Graph DFS)
Count connected groups of '1's in a grid.
python
def num_islands(grid): if not grid: return 0 rows, cols = len(grid), len(grid[0]) visited = set() def dfs(r, c): if (r < 0 or c < 0 or r >= rows or c >= cols or grid[r][c] == '0' or (r, c) in visited): return visited.add((r, c)) directions = [(1,0), (-1,0), (0,1), (0,-1)] for dr, dc in directions: dfs(r+dr, c+dc) islands = 0 for r in range(rows): for c in range(cols): if grid[r][c] == '1' and (r, c) not in visited: dfs(r, c) islands += 1 return islands
These coding challenges emphasize algorithmic thinking, optimization, and Python-specific data structures and libraries for senior-level developers. Preparing these types of questions along with system design and real-world problem-solving examples is ideal for senior Python developer interviews.