← Back to Portfolio NEW 2026 ADVANCED EDITION / OCTOBER 2026

DSA Sheet

New: 100 hard, company-specific problems (Google, Amazon, Microsoft, Meta and more) with key insights, step-by-step approaches and tested Python solutions. Plus 99 original solutions, 24 worked Python problems, company-preparation tracks, and a downloadable PDF. All included in the same ₹49 purchase.

100
Advanced 2026 problems
174
Practice questions
18
Core patterns
99
Original paid notes

πŸ”’ Unlock Full DSA Sheet

Get the complete original solution sheet and the expanded practice library with one purchase.

β‚Ή49
β‚Ή499

Already have an access code?

Original DSA sheet: foundationsThe original 99 problems remain included as warm-up and revision material.

πŸ“¦ Arrays & Strings (15 problems)
#ProblemDifficultyPatternCompaniesSolution
1Two SumEasyHash MapGoogleAmazonMicrosoftShow
IDEA: Store each number's complement in a hash map. For each number, check if its complement exists. def twoSum(nums, target): seen = {} for i, n in enumerate(nums): comp = target - n if comp in seen: return [seen[comp], i] seen[n] = i Time: O(n), Space: O(n)
2Best Time to Buy and Sell StockEasyGreedyGoogleAmazonMetaShow
IDEA: Track the minimum price seen so far. At each price, calculate profit if we sold today. def maxProfit(prices): min_p = float('inf') max_prof = 0 for p in prices: min_p = min(min_p, p) max_prof = max(max_prof, p - min_p) return max_prof Time: O(n), Space: O(1)
3Contains DuplicateEasyHash SetMicrosoftAmazonShow
IDEA: Put all numbers in a set. If set size differs from array size, duplicates exist. def containsDuplicate(nums): return len(nums) != len(set(nums)) Time: O(n), Space: O(n)
4Product of Array Except SelfMediumPrefix SumGoogleMicrosoftAmazonShow
IDEA: Two passes β€” build left products, then multiply by right products. No division needed. def productExceptSelf(nums): n = len(nums) res = [1] * n left = 1 for i in range(n): res[i] = left left *= nums[i] right = 1 for i in range(n-1, -1, -1): res[i] *= right right *= nums[i] return res Time: O(n), Space: O(1) extra
5Maximum Subarray (Kadane's)MediumDP / GreedyAmazonMetaGoogleShow
IDEA: Kadane's β€” extend current subarray or start fresh. Keep running sum, reset if it drops below current element. def maxSubArray(nums): max_sum = cur = nums[0] for n in nums[1:]: cur = max(n, cur + n) max_sum = max(max_sum, cur) return max_sum Time: O(n), Space: O(1)
63SumMediumTwo PointersMicrosoftGoogleAmazonShow
IDEA: Sort array. Fix one number, use two pointers on remaining to find pairs summing to its negative. Skip duplicates. def threeSum(nums): nums.sort() res = [] for i in range(len(nums)-2): if i > 0 and nums[i] == nums[i-1]: continue l, r = i+1, len(nums)-1 while l < r: s = nums[i] + nums[l] + nums[r] if s < 0: l += 1 elif s > 0: r -= 1 else: res.append([nums[i], nums[l], nums[r]]) while l < r and nums[l] == nums[l+1]: l += 1 l += 1; r -= 1 return res Time: O(nu00b2), Space: O(1)
7Container With Most WaterMediumTwo PointersGoogleMetaShow
IDEA: Two pointers at both ends. Area = min(heights) * width. Move the shorter pointer inward β€” it limits the height. def maxArea(height): l, r = 0, len(height) - 1 best = 0 while l < r: best = max(best, min(height[l], height[r]) * (r - l)) if height[l] < height[r]: l += 1 else: r -= 1 return best Time: O(n), Space: O(1)
8Merge IntervalsMediumSortingAmazonMicrosoftGoogleShow
IDEA: Sort by start time. Merge overlapping by comparing current end with next start. def merge(intervals): intervals.sort(key=lambda x: x[0]) res = [intervals[0]] for start, end in intervals[1:]: if start <= res[-1][1]: res[-1][1] = max(res[-1][1], end) else: res.append([start, end]) return res Time: O(n log n), Space: O(n)
9Longest Substring Without Repeating CharactersMediumSliding WindowGoogleAmazonMetaShow
IDEA: Sliding window β€” expand right, if duplicate shrink left. Track max window size. def lengthOfLongestSubstring(s): seen = set() l = res = 0 for r in range(len(s)): while s[r] in seen: seen.remove(s[l]); l += 1 seen.add(s[r]) res = max(res, r - l + 1) return res Time: O(n), Space: O(min(n, 26))
10Valid AnagramEasyHash MapMicrosoftAmazonShow
IDEA: Count character frequencies in both strings. If counts match, they're anagrams. def isAnagram(s, t): if len(s) != len(t): return False count = {} for c in s: count[c] = count.get(c, 0) + 1 for c in t: count[c] = count.get(c, 0) - 1 if count[c] < 0: return False return True Time: O(n), Space: O(1) β€” at most 26 letters
11Group AnagramsMediumHash MapGoogleAmazonMetaShow
IDEA: Anagrams produce the same sorted string. Use sorted word as dictionary key to group. def groupAnagrams(strs): groups = {} for s in strs: key = ''.join(sorted(s)) groups.setdefault(key, []).append(s) return list(groups.values()) Time: O(n * k log k), Space: O(n * k)
12Trapping Rain WaterHardTwo PointersStackAmazonGoogleMicrosoftShow
IDEA: Water at each bar = min(maxLeft, maxRight) - height. Two pointers tracking max from each side. def trap(height): l, r = 0, len(height) - 1 lmax = rmax = res = 0 while l < r: if height[l] < height[r]: if height[l] >= lmax: lmax = height[l] else: res += lmax - height[l] l += 1 else: if height[r] >= rmax: rmax = height[r] else: res += rmax - height[r] r -= 1 return res Time: O(n), Space: O(1)
13Minimum Window SubstringHardSliding WindowMetaGoogleShow
IDEA: Expand window right until all chars of t are covered, then shrink left to find minimum. def minWindow(s, t): need = {} for c in t: need[c] = need.get(c, 0) + 1 required = len(need); have = 0 window = {}; l = 0; res = ""; resLen = float('inf') for r in range(len(s)): c = s[r] window[c] = window.get(c, 0) + 1 if c in need and window[c] == need[c]: have += 1 while have == required: if (r - l + 1) < resLen: res = s[l:r+1]; resLen = r - l + 1 window[s[l]] -= 1 if s[l] in need and window[s[l]] < need[s[l]]: have -= 1 l += 1 return res Time: O(n), Space: O(n)
14Longest Palindromic SubstringMediumExpand CenterGoogleMicrosoftShow
IDEA: For each index, expand outward while chars match. Check both odd (single center) and even (two centers). def longestPalindrome(s): res = "" def expand(l, r): nonlocal res while l >= 0 and r < len(s) and s[l] == s[r]: if r - l + 1 > len(res): res = s[l:r+1] l -= 1; r += 1 for i in range(len(s)): expand(i, i) # odd length expand(i, i + 1) # even length return res Time: O(nu00b2), Space: O(1)
15Rotate ArrayMediumArrayAmazonMicrosoftShow
IDEA: Three reverses = rotation. Reverse all, reverse first k, reverse rest. All in-place. def rotate(nums, k): k %= len(nums) def rev(l, r): while l < r: nums[l], nums[r] = nums[r], nums[l] l += 1; r -= 1 rev(0, len(nums) - 1) rev(0, k - 1) rev(k, len(nums) - 1) Time: O(n), Space: O(1)
πŸ”— Linked Lists (6 problems)
#ProblemDifficultyPatternCompaniesSolution
16Reverse Linked ListEasyIterativeMicrosoftAmazonShow
IDEA: Walk through list. At each node, point next to previous. Track prev, curr, nxt. def reverseList(head): prev = None curr = head while curr: nxt = curr.next curr.next = prev prev = curr curr = nxt return prev Time: O(n), Space: O(1)
17Merge Two Sorted ListsEasyTwo PointersAmazonMicrosoftShow
IDEA: Dummy head node. Compare both lists, attach smaller node. Advance that pointer. def mergeTwoLists(l1, l2): dummy = ListNode() curr = dummy while l1 and l2: if l1.val <= l2.val: curr.next = l1; l1 = l1.next else: curr.next = l2; l2 = l2.next curr = curr.next curr.next = l1 or l2 return dummy.next Time: O(n + m), Space: O(1)
18Linked List Cycle (Floyd's)EasyFast & SlowGoogleMicrosoftShow
IDEA: Slow pointer moves 1 step, fast moves 2. If they ever meet, there's a cycle. def hasCycle(head): slow = fast = head while fast and fast.next: slow = slow.next fast = fast.next.next if slow == fast: return True return False Time: O(n), Space: O(1)
19Remove Nth Node From EndMediumTwo PointersAmazonMetaShow
IDEA: Advance fast pointer n+1 steps ahead. Move both until fast=null. Slow is before target. def removeNthFromEnd(head, n): dummy = ListNode(0, head) slow = fast = dummy for _ in range(n + 1): fast = fast.next while fast: slow = slow.next; fast = fast.next slow.next = slow.next.next return dummy.next Time: O(n), Space: O(1)
20Merge K Sorted ListsHardHeapGoogleAmazonShow
IDEA: Min-heap picks smallest head among k lists. Pop min, push its next node back. import heapq def mergeKLists(lists): heap = [] for i, l in enumerate(lists): if l: heapq.heappush(heap, (l.val, i, l)) dummy = curr = ListNode() while heap: val, i, node = heapq.heappop(heap) curr.next = node; curr = curr.next if node.next: heapq.heappush(heap, (node.next.val, i, node.next)) return dummy.next Time: O(N log k), Space: O(k)
21LRU CacheMediumHash Map + DLLMicrosoftGoogleAmazonShow
IDEA: HashMap for O(1) lookup + Doubly Linked List for O(1) eviction. Most recent at tail. class Node: def __init__(self, key, val): self.key, self.val = key, val self.prev = self.next = None class LRUCache: def __init__(self, cap): self.cache = {} self.cap = cap self.head, self.tail = Node(0,0), Node(0,0) self.head.next, self.tail.prev = self.tail, self.head def _remove(self, node): node.prev.next, node.next.prev = node.next, node.prev def _add(self, node): # add before tail node.prev, node.next = self.tail.prev, self.tail self.tail.prev.next = self.tail.prev = node def get(self, key): if key in self.cache: node = self.cache[key] self._remove(node) self._add(node) return node.val return -1 def put(self, key, val): if key in self.cache: self._remove(self.cache[key]) node = Node(key, val) self._add(node) self.cache[key] = node if len(self.cache) > self.cap: lru = self.head.next self._remove(lru) del self.cache[lru.key] Time: O(1) get/put, Space: O(capacity)
πŸ“š Stacks & Queues (4 problems)
#ProblemDifficultyPatternCompaniesSolution
22Valid ParenthesesEasyStackAmazonMicrosoftShow
IDEA: Push open brackets onto stack. For closing, check if top matches. Stack must be empty at end. def isValid(s): stack = [] pairs = {')':'(', ']':'[', '}':'{'} for c in s: if c in pairs: if not stack or stack[-1] != pairs[c]: return False stack.pop() else: stack.append(c) return len(stack) == 0 Time: O(n), Space: O(n)
23Min StackMediumDesignGoogleAmazonShow
IDEA: Two stacks β€” main + min stack. Min stack always holds current minimum at each level. class MinStack: def __init__(self): self.stack = [] self.minStack = [] def push(self, val): self.stack.append(val) m = min(val, self.minStack[-1] if self.minStack else val) self.minStack.append(m) def pop(self): self.stack.pop() self.minStack.pop() def top(self): return self.stack[-1] def getMin(self): return self.minStack[-1] All operations O(1), Space: O(n)
24Daily TemperaturesMediumMonotonic StackAmazonGoogleShow
IDEA: Monotonic decreasing stack of indices. When warmer day comes, pop cooler days, record gap. def dailyTemperatures(temps): res = [0] * len(temps) stack = [] for i, t in enumerate(temps): while stack and temps[stack[-1]] < t: j = stack.pop() res[j] = i - j stack.append(i) return res Time: O(n), Space: O(n)
25Largest Rectangle in HistogramHardMonotonic StackGoogleMetaShow
IDEA: Stack tracks ascending bar heights. When a shorter bar comes, pop and calculate areas. def largestRectangleArea(heights): stack = [] # (index, height) maxArea = 0 for i, h in enumerate(heights): start = i while stack and stack[-1][1] > h: idx, ht = stack.pop() maxArea = max(maxArea, ht * (i - idx)) start = idx stack.append((start, h)) for idx, ht in stack: maxArea = max(maxArea, ht * (len(heights) - idx)) return maxArea Time: O(n), Space: O(n)
πŸ” Binary Search (4 problems)
#ProblemDifficultyPatternCompaniesSolution
26Binary SearchEasyBinary SearchGoogleMicrosoftShow
IDEA: Compare middle element with target. Smaller β†’ search right. Larger β†’ search left. def search(nums, target): l, r = 0, len(nums) - 1 while l <= r: mid = (l + r) // 2 if nums[mid] == target: return mid elif nums[mid] < target: l = mid + 1 else: r = mid - 1 return -1 Time: O(log n), Space: O(1)
27Search in Rotated Sorted ArrayMediumModified BSMicrosoftAmazonGoogleShow
IDEA: Determine which half is sorted. Check if target lies in the sorted half. Narrow search. def search(nums, target): l, r = 0, len(nums) - 1 while l <= r: mid = (l + r) // 2 if nums[mid] == target: return mid if nums[l] <= nums[mid]: # left sorted if nums[l] <= target < nums[mid]: r = mid - 1 else: l = mid + 1 else: # right sorted if nums[mid] < target <= nums[r]: l = mid + 1 else: r = mid - 1 return -1 Time: O(log n), Space: O(1)
28Find Minimum in Rotated Sorted ArrayMediumModified BSGoogleAmazonShow
IDEA: If mid > right, rotation point is to the right. Otherwise it's to the left or at mid. def findMin(nums): l, r = 0, len(nums) - 1 while l < r: mid = (l + r) // 2 if nums[mid] > nums[r]: l = mid + 1 else: r = mid return nums[l] Time: O(log n), Space: O(1)
29Median of Two Sorted ArraysHardBinary SearchGoogleMetaAmazonShow
IDEA: Binary search on smaller array to find correct partition. All left elements must be ≀ all right. def findMedianSortedArrays(A, B): if len(A) > len(B): A, B = B, A m, n = len(A), len(B) l, r = 0, m while l <= r: i = (l + r) // 2 j = (m + n + 1) // 2 - i aL = A[i-1] if i > 0 else float('-inf') aR = A[i] if i < m else float('inf') bL = B[j-1] if j > 0 else float('-inf') bR = B[j] if j < n else float('inf') if aL <= bR and bL <= aR: if (m+n) % 2: return max(aL, bL) return (max(aL,bL) + min(aR,bR)) / 2 elif aL > bR: r = i - 1 else: l = i + 1 Time: O(log(min(m,n))), Space: O(1)
🌲 Trees & BST (6 problems)
#ProblemDifficultyPatternCompaniesSolution
30Invert Binary TreeEasyDFSMicrosoftAmazonShow
IDEA: Recursively swap left and right children of every node. def invertTree(root): if not root: return None root.left, root.right = root.right, root.left invertTree(root.left) invertTree(root.right) return root Time: O(n), Space: O(h) recursion depth
31Maximum Depth of Binary TreeEasyDFSGoogleMicrosoftShow
IDEA: Depth = 1 + max(left subtree depth, right subtree depth). Null node = depth 0. def maxDepth(root): if not root: return 0 return 1 + max(maxDepth(root.left), maxDepth(root.right)) Time: O(n), Space: O(h)
32Validate BSTMediumDFSAmazonMetaShow
IDEA: Every node must be within (lo, hi) range. Left child tightens upper bound, right tightens lower. def isValidBST(root): def check(node, lo, hi): if not node: return True if node.val <= lo or node.val >= hi: return False return (check(node.left, lo, node.val) and check(node.right, node.val, hi)) return check(root, float('-inf'), float('inf')) Time: O(n), Space: O(h)
33Lowest Common Ancestor of BSTMediumBST PropertyGoogleAmazonShow
IDEA: In BST, if both values smaller go left, both larger go right. Split point = LCA. def lowestCommonAncestor(root, p, q): while root: if p.val < root.val and q.val < root.val: root = root.left elif p.val > root.val and q.val > root.val: root = root.right else: return root Time: O(h), Space: O(1)
34Binary Tree Level Order TraversalMediumBFSGoogleMetaShow
IDEA: BFS using a queue. Process all nodes at current level, enqueue their children for next. from collections import deque def levelOrder(root): if not root: return [] res, q = [], deque([root]) while q: level = [] for _ in range(len(q)): node = q.popleft() level.append(node.val) if node.left: q.append(node.left) if node.right: q.append(node.right) res.append(level) return res Time: O(n), Space: O(n)
35Serialize and Deserialize Binary TreeHardBFS/DFSGoogleAmazonMetaShow
IDEA: Preorder DFS to serialize (val or 'N' for null). Deserialize reads in same order. class Codec: def serialize(self, root): res = [] def dfs(node): if not node: res.append('N'); return res.append(str(node.val)) dfs(node.left); dfs(node.right) dfs(root) return ','.join(res) def deserialize(self, data): vals = iter(data.split(',')) def dfs(): v = next(vals) if v == 'N': return None node = TreeNode(int(v)) node.left = dfs() node.right = dfs() return node return dfs() Time: O(n), Space: O(n)
πŸ•ΈοΈ Graphs (5 problems)
#ProblemDifficultyPatternCompaniesSolution
36Number of IslandsMediumBFS/DFSAmazonMicrosoftGoogleShow
IDEA: Scan for '1'. When found, DFS to mark all connected '1's as visited. Count DFS starts. def numIslands(grid): count = 0 def dfs(i, j): if (i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]) or grid[i][j] != '1'): return grid[i][j] = '0' dfs(i+1,j); dfs(i-1,j); dfs(i,j+1); dfs(i,j-1) for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] == '1': count += 1; dfs(i, j) return count Time: O(m*n), Space: O(m*n) recursion
37Clone GraphMediumBFS + HashMapGoogleMetaShow
IDEA: BFS with HashMap {original node β†’ clone}. Create clones, then wire up neighbors. def cloneGraph(node): if not node: return None clones = {node: Node(node.val)} q = deque([node]) while q: curr = q.popleft() for nei in curr.neighbors: if nei not in clones: clones[nei] = Node(nei.val) q.append(nei) clones[curr].neighbors.append(clones[nei]) return clones[node] Time: O(V + E), Space: O(V)
38Course Schedule (Topological Sort)MediumTopological SortAmazonGoogleShow
IDEA: Topological sort (Kahn's BFS). Build indegree map. Process zero-indegree nodes. Cycle = can't finish. def canFinish(numCourses, prereqs): graph = [[] for _ in range(numCourses)] indegree = [0] * numCourses for a, b in prereqs: graph[b].append(a) indegree[a] += 1 q = deque([i for i in range(numCourses) if indegree[i] == 0]) count = 0 while q: node = q.popleft(); count += 1 for nei in graph[node]: indegree[nei] -= 1 if indegree[nei] == 0: q.append(nei) return count == numCourses Time: O(V + E), Space: O(V + E)
39Word LadderHardBFSGoogleMicrosoftShow
IDEA: BFS from beginWord. Each step tries changing each char to a-z. Track visited words. def ladderLength(begin, end, wordList): wordSet = set(wordList) if end not in wordSet: return 0 q = deque([(begin, 1)]) visited = {begin} while q: word, steps = q.popleft() for i in range(len(word)): for c in 'abcdefghijklmnopqrstuvwxyz': nw = word[:i] + c + word[i+1:] if nw == end: return steps + 1 if nw in wordSet and nw not in visited: visited.add(nw) q.append((nw, steps + 1)) return 0 Time: O(n * m * 26), Space: O(n * m)
40Network Delay Time (LC #743)MediumDijkstra / Shortest PathGoogleAmazonMicrosoftShow
IDEA: Dijkstra from source node k. Find shortest time to reach all nodes. Answer = max of all distances. import heapq def networkDelayTime(times, n, k): graph = [[] for _ in range(n + 1)] for u, v, w in times: graph[u].append((v, w)) dist = [float('inf')] * (n + 1) dist[k] = 0 heap = [(0, k)] while heap: d, u = heapq.heappop(heap) if d > dist[u]: continue for v, w in graph[u]: if dist[u] + w < dist[v]: dist[v] = dist[u] + w heapq.heappush(heap, (dist[v], v)) mx = max(dist[1:]) return mx if mx < float('inf') else -1 Time: O(E log V), Space: O(V + E)
🧩 Dynamic Programming (10 problems)
#ProblemDifficultyPatternCompaniesSolution
41Climbing StairsEasy1D DPGoogleAmazonShow
IDEA: Fibonacci β€” ways(n) = ways(n-1) + ways(n-2). Reach step n from n-1 or n-2. def climbStairs(n): a, b = 1, 1 for _ in range(n - 1): a, b = b, a + b return b Time: O(n), Space: O(1)
42House RobberMedium1D DPAmazonMicrosoftShow
IDEA: At each house: max of skip (prev best) or rob (prev-prev + current). Can't rob adjacent. def rob(nums): prev2, prev1 = 0, 0 for n in nums: prev2, prev1 = prev1, max(prev1, prev2 + n) return prev1 Time: O(n), Space: O(1)
43Coin ChangeMediumUnbounded KnapsackGoogleAmazonShow
IDEA: dp[amount] = min coins. For each amount, try every coin: dp[a] = min(dp[a], dp[a-coin]+1). def coinChange(coins, amount): dp = [float('inf')] * (amount + 1) dp[0] = 0 for a in range(1, amount + 1): for c in coins: if c <= a: dp[a] = min(dp[a], dp[a - c] + 1) return dp[amount] if dp[amount] != float('inf') else -1 Time: O(amount * coins), Space: O(amount)
44Longest Common SubsequenceMedium2D DPGoogleMicrosoftShow
IDEA: If chars match, dp[i][j] = dp[i-1][j-1]+1. Else take max of skipping from either string. def longestCommonSubsequence(t1, t2): m, n = len(t1), len(t2) dp = [[0]*(n+1) for _ in range(m+1)] for i in range(1, m+1): for j in range(1, n+1): if t1[i-1] == t2[j-1]: dp[i][j] = dp[i-1][j-1] + 1 else: dp[i][j] = max(dp[i-1][j], dp[i][j-1]) return dp[m][n] Time: O(m*n), Space: O(m*n)
45Longest Increasing SubsequenceMediumBinary Search + DPAmazonGoogleShow
IDEA: Maintain sorted 'tails' array. Binary search for position of each num. Append or replace. import bisect def lengthOfLIS(nums): tails = [] for n in nums: pos = bisect.bisect_left(tails, n) if pos == len(tails): tails.append(n) else: tails[pos] = n return len(tails) Time: O(n log n), Space: O(n)
46Word BreakMediumDPGoogleMetaAmazonShow
IDEA: dp[i] = can s[0:i] be segmented? For each position, check if any word in dict ends there. def wordBreak(s, wordDict): dp = [False] * (len(s) + 1) dp[0] = True for i in range(1, len(s) + 1): for w in wordDict: if (i >= len(w) and dp[i - len(w)] and s[i-len(w):i] == w): dp[i] = True; break return dp[len(s)] Time: O(n * m * k), Space: O(n)
47Edit DistanceMedium2D DPGoogleAmazonShow
IDEA: dp[i][j] = min edits for word1[0..i] to word2[0..j]. Match = no cost. Else min(insert, delete, replace)+1. def minDistance(w1, w2): m, n = len(w1), len(w2) dp = [[0]*(n+1) for _ in range(m+1)] for i in range(m+1): dp[i][0] = i for j in range(n+1): dp[0][j] = j for i in range(1, m+1): for j in range(1, n+1): if w1[i-1] == w2[j-1]: dp[i][j] = dp[i-1][j-1] else: dp[i][j] = 1 + min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) return dp[m][n] Time: O(m*n), Space: O(m*n)
48Last Stone Weight II (LC #1049)Medium0/1 Knapsack DPAmazonGoogleMetaShow
IDEA: Split stones into two groups. Minimize |sum1 - sum2|. This is 0/1 Knapsack β€” find closest subset sum to total/2. def lastStoneWeightII(stones): total = sum(stones) target = total // 2 dp = {0} for s in stones: dp = dp | {t + s for t in dp if t + s <= target} return total - 2 * max(dp) Time: O(n * sum/2), Space: O(sum/2)
49Burst BalloonsHardInterval DPGoogleShow
IDEA: Think reverse β€” which balloon to burst LAST in range? dp[l][r] = max coins in (l,r). def maxCoins(nums): nums = [1] + nums + [1] n = len(nums) dp = [[0]*n for _ in range(n)] for length in range(2, n): for l in range(n - length): r = l + length for k in range(l+1, r): dp[l][r] = max(dp[l][r], dp[l][k] + dp[k][r] + nums[l]*nums[k]*nums[r]) return dp[0][n-1] Time: O(nu00b3), Space: O(nu00b2)
50Regular Expression MatchingHard2D DPGoogleAmazonMetaShow
IDEA: dp[i][j] = does s[0..i] match p[0..j]? Handle '.' (any char) and '*' (zero or more of prev). def isMatch(s, p): m, n = len(s), len(p) dp = [[False]*(n+1) for _ in range(m+1)] dp[0][0] = True for j in range(2, n+1): if p[j-1] == '*': dp[0][j] = dp[0][j-2] for i in range(1, m+1): for j in range(1, n+1): if p[j-1] == '.' or p[j-1] == s[i-1]: dp[i][j] = dp[i-1][j-1] elif p[j-1] == '*': dp[i][j] = dp[i][j-2] # zero if p[j-2]=='.' or p[j-2]==s[i-1]: dp[i][j] |= dp[i-1][j] # one+ return dp[m][n] Time: O(m*n), Space: O(m*n)
⛰️ Heaps & Priority Queues (3 problems)
#ProblemDifficultyPatternCompaniesSolution
51Top K Frequent ElementsMediumHeap / Bucket SortAmazonGoogleShow
IDEA: Count frequencies. Bucket sort β€” index=frequency, value=list of nums. Scan from highest. def topKFrequent(nums, k): count = {} for n in nums: count[n] = count.get(n, 0) + 1 buckets = [[] for _ in range(len(nums) + 1)] for num, freq in count.items(): buckets[freq].append(num) res = [] for i in range(len(buckets)-1, 0, -1): for n in buckets[i]: res.append(n) if len(res) == k: return res return res Time: O(n), Space: O(n)
52Find Median from Data StreamHardTwo HeapsGoogleAmazonShow
IDEA: Max-heap for lower half, min-heap for upper half. Balance sizes. Median from heap tops. import heapq class MedianFinder: def __init__(self): self.lo = [] # max-heap (negate) self.hi = [] # min-heap def addNum(self, num): heapq.heappush(self.lo, -num) heapq.heappush(self.hi, -heapq.heappop(self.lo)) if len(self.hi) > len(self.lo): heapq.heappush(self.lo, -heapq.heappop(self.hi)) def findMedian(self): if len(self.lo) > len(self.hi): return -self.lo[0] return (-self.lo[0] + self.hi[0]) / 2 Time: O(log n) add, O(1) find
53Kth Largest ElementMediumQuick Select / HeapMetaGoogleShow
IDEA: QuickSelect β€” partition, if pivot at k done, else recurse on correct side. Avg O(n). def findKthLargest(nums, k): k = len(nums) - k def qs(l, r): pivot, p = nums[r], l for i in range(l, r): if nums[i] <= pivot: nums[p], nums[i] = nums[i], nums[p] p += 1 nums[p], nums[r] = nums[r], nums[p] if p == k: return nums[p] elif p < k: return qs(p+1, r) else: return qs(l, p-1) return qs(0, len(nums)-1) Time: O(n) avg, O(nu00b2) worst, Space: O(1)
πŸ”€ Tries (2 problems)
#ProblemDifficultyPatternCompaniesSolution
54Implement Trie (Prefix Tree)MediumDesignGoogleMicrosoftShow
IDEA: Tree of nodes, each with children dict + isEnd flag. Insert creates path, search follows it. class TrieNode: def __init__(self): self.children = {} self.isEnd = False class Trie: def __init__(self): self.root = TrieNode() def insert(self, word): node = self.root for c in word: if c not in node.children: node.children[c] = TrieNode() node = node.children[c] node.isEnd = True def search(self, word): node = self.root for c in word: if c not in node.children: return False node = node.children[c] return node.isEnd def startsWith(self, prefix): node = self.root for c in prefix: if c not in node.children: return False node = node.children[c] return True Time: O(m) per op, Space: O(total chars)
55Word Search IIHardTrie + BacktrackingGoogleAmazonShow
IDEA: Build trie from word list. DFS on board following trie. Prune trie to avoid duplicates. def findWords(board, words): root = {} for w in words: node = root for c in w: node = node.setdefault(c, {}) node['#'] = w res = [] def dfs(i, j, node): if '#' in node: res.append(node.pop('#')) if i<0 or j<0 or i>=len(board) or j>=len(board[0]): return c = board[i][j] if c not in node: return board[i][j] = '.' for di,dj in [(0,1),(0,-1),(1,0),(-1,0)]: dfs(i+di, j+dj, node[c]) board[i][j] = c for i in range(len(board)): for j in range(len(board[0])): dfs(i, j, root) return res Time: O(m*n*4^L), Space: O(word chars)
🎯 Greedy (2 problems)
#ProblemDifficultyPatternCompaniesSolution
56Jump GameMediumGreedyAmazonGoogleShow
IDEA: Track farthest reachable index. If current index exceeds farthest, you're stuck. def canJump(nums): farthest = 0 for i in range(len(nums)): if i > farthest: return False farthest = max(farthest, i + nums[i]) return True Time: O(n), Space: O(1)
57Task SchedulerMediumGreedy / HeapGoogleAmazonMetaShow
IDEA: Most frequent task determines idle time. Fill idle slots with other tasks. def leastInterval(tasks, n): freq = {} for t in tasks: freq[t] = freq.get(t, 0) + 1 maxFreq = max(freq.values()) maxCount = sum(1 for v in freq.values() if v == maxFreq) result = (maxFreq - 1) * (n + 1) + maxCount return max(result, len(tasks)) Time: O(n), Space: O(1)
πŸ”™ Backtracking (3 problems)
#ProblemDifficultyPatternCompaniesSolution
58SubsetsMediumBacktrackingGoogleAmazonShow
IDEA: For each element, include or exclude. Backtrack: add, recurse, remove. def subsets(nums): res = [] def bt(start, path): res.append(path[:]) for i in range(start, len(nums)): path.append(nums[i]) bt(i + 1, path) path.pop() bt(0, []) return res Time: O(n * 2^n), Space: O(n)
59PermutationsMediumBacktrackingGoogleMetaShow
IDEA: For each position, try every unused number. Track used with a set. def permute(nums): res = [] def bt(path, used): if len(path) == len(nums): res.append(path[:]); return for i in range(len(nums)): if i in used: continue used.add(i); path.append(nums[i]) bt(path, used) path.pop(); used.remove(i) bt([], set()) return res Time: O(n * n!), Space: O(n)
60N-QueensHardBacktrackingGoogleAmazonShow
IDEA: Place queens row by row. Track attacked cols, diags (r-c), anti-diags (r+c) with sets. def solveNQueens(n): res = [] cols, diag, anti = set(), set(), set() board = [['.']*n for _ in range(n)] def bt(r): if r == n: res.append([''.join(row) for row in board]) return for c in range(n): if c in cols or (r-c) in diag or (r+c) in anti: continue cols.add(c); diag.add(r-c); anti.add(r+c) board[r][c] = 'Q' bt(r + 1) board[r][c] = '.' cols.remove(c); diag.remove(r-c); anti.remove(r+c) bt(0) return res Time: O(n!), Space: O(nu00b2)
⚑ Bit Manipulation (3 problems)
#ProblemDifficultyPatternCompaniesSolution
61Single Number (XOR)EasyXORGoogleAmazonShow
IDEA: XOR cancels pairs (a^a=0, a^0=a). XOR all elements β€” result is the unique number. def singleNumber(nums): res = 0 for n in nums: res ^= n return res Time: O(n), Space: O(1)
62Number of 1 BitsEasyBit CountMicrosoftAppleShow
IDEA: n & (n-1) clears the lowest set bit. Count iterations until n = 0. def hammingWeight(n): count = 0 while n: n &= n - 1 count += 1 return count Time: O(k) where k = set bits, Space: O(1)
63Reverse BitsEasyBit ManipulationGoogleAppleShow
IDEA: Extract each bit from right, shift result left and OR the bit in. def reverseBits(n): res = 0 for _ in range(32): res = (res << 1) | (n & 1) n >>= 1 return res Time: O(32) = O(1), Space: O(1)
πŸ“ Math & Geometry (3 problems)
#ProblemDifficultyPatternCompaniesSolution
64Rotate Image (Matrix)MediumMatrixGoogleMicrosoftShow
IDEA: Transpose matrix (swap rows/cols), then reverse each row. Rotates 90u00b0 clockwise. def rotate(matrix): n = len(matrix) for i in range(n): for j in range(i+1, n): matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j] for row in matrix: row.reverse() Time: O(nu00b2), Space: O(1)
65Spiral MatrixMediumMatrixAmazonMicrosoftShow
IDEA: 4 boundaries (top, bottom, left, right). Traverse right-down-left-up, shrink after each. def spiralOrder(matrix): res = [] top, bot = 0, len(matrix)-1 left, right = 0, len(matrix[0])-1 while top <= bot and left <= right: for j in range(left, right+1): res.append(matrix[top][j]) top += 1 for i in range(top, bot+1): res.append(matrix[i][right]) right -= 1 if top <= bot: for j in range(right, left-1, -1): res.append(matrix[bot][j]) bot -= 1 if left <= right: for i in range(bot, top-1, -1): res.append(matrix[i][left]) left += 1 return res Time: O(m*n), Space: O(1)
66Pow(x, n)MediumFast ExponentiationGoogleMetaAmazonShow
IDEA: Binary exponentiation β€” if n even: x^n = (xu00b2)^(n/2). If odd: x * x^(n-1). Handle negative n. def myPow(x, n): if n < 0: x, n = 1/x, -n res = 1 while n: if n % 2: res *= x x *= x n //= 2 return res Time: O(log n), Space: O(1)
πŸ“… Intervals (3 problems)
#ProblemDifficultyPatternCompaniesSolution
67Insert IntervalMediumIntervalsAmazonGoogleShow
IDEA: Three phases β€” add intervals before new, merge overlapping with new, add rest after. def insert(intervals, new): res = [] for i, intv in enumerate(intervals): if intv[1] < new[0]: res.append(intv) elif intv[0] > new[1]: res.append(new) return res + intervals[i:] else: new = [min(new[0], intv[0]), max(new[1], intv[1])] res.append(new) return res Time: O(n), Space: O(n)
68Meeting Rooms IIMediumHeap / SortingGoogleMetaAmazonShow
IDEA: Sort by start. Min-heap of end times. If earliest room is free (end u2264 start), reuse; else new room. import heapq def minMeetingRooms(intervals): intervals.sort() heap = [] for start, end in intervals: if heap and heap[0] <= start: heapq.heappop(heap) heapq.heappush(heap, end) return len(heap) Time: O(n log n), Space: O(n)
69Non-overlapping IntervalsMediumGreedyAmazonGoogleShow
IDEA: Sort by end time. Greedily keep non-overlapping intervals. Count removals. def eraseOverlapIntervals(intervals): intervals.sort(key=lambda x: x[1]) count = 0; prevEnd = float('-inf') for start, end in intervals: if start >= prevEnd: prevEnd = end else: count += 1 return count Time: O(n log n), Space: O(1)
πŸͺŸ Sliding Window (4 problems)
#ProblemDifficultyPatternCompaniesSolution
70Maximum Average Subarray I (LC #643)EasyFixed WindowAmazonGoogleMicrosoftShow
IDEA: Fixed window of size k. Add right element, subtract left. Track max sum, return max/k. def findMaxAverage(nums, k): wSum = sum(nums[:k]) maxSum = wSum for i in range(k, len(nums)): wSum += nums[i] - nums[i - k] maxSum = max(maxSum, wSum) return maxSum / k Time: O(n), Space: O(1)
71Longest Repeating Character ReplacementMediumVariable WindowGoogleMetaShow
IDEA: Sliding window. Window is valid if windowSize - maxFreq u2264 k (can replace u2264 k chars). def characterReplacement(s, k): count = {}; l = maxFreq = res = 0 for r in range(len(s)): count[s[r]] = count.get(s[r], 0) + 1 maxFreq = max(maxFreq, count[s[r]]) while (r - l + 1) - maxFreq > k: count[s[l]] -= 1; l += 1 res = max(res, r - l + 1) return res Time: O(n), Space: O(1)
72Permutation in StringMediumFixed WindowGoogleAmazonShow
IDEA: Fixed window of size len(s1). Compare char frequencies of window with s1. Match = True. def checkInclusion(s1, s2): if len(s1) > len(s2): return False c1, c2 = {}, {} for c in s1: c1[c] = c1.get(c,0)+1 for i in range(len(s2)): c2[s2[i]] = c2.get(s2[i],0)+1 if i >= len(s1): c2[s2[i-len(s1)]] -= 1 if c2[s2[i-len(s1)]] == 0: del c2[s2[i-len(s1)]] if c1 == c2: return True return False Time: O(n), Space: O(1)
73Sliding Window MaximumHardMonotonic DequeMetaGoogleAmazonShow
IDEA: Monotonic decreasing deque. Front = max of window. Remove out-of-window and smaller elements. from collections import deque def maxSlidingWindow(nums, k): dq = deque() # indices res = [] for i in range(len(nums)): while dq and dq[0] < i - k + 1: dq.popleft() while dq and nums[dq[-1]] < nums[i]: dq.pop() dq.append(i) if i >= k - 1: res.append(nums[dq[0]]) return res Time: O(n), Space: O(k)
πŸ”— Union Find (3 problems)
#ProblemDifficultyPatternCompaniesSolution
74Number of Connected ComponentsMediumUnion FindGoogleAmazonShow
IDEA: Union-Find with path compression + rank. Start with n components, decrement on each union. def countComponents(n, edges): parent = list(range(n)) rank = [0] * n def find(x): while parent[x] != x: parent[x] = parent[parent[x]] x = parent[x] return x def union(a, b): ra, rb = find(a), find(b) if ra == rb: return 0 if rank[ra] < rank[rb]: ra, rb = rb, ra parent[rb] = ra if rank[ra] == rank[rb]: rank[ra] += 1 return 1 return n - sum(union(a,b) for a,b in edges) Time: O(n * u03b1(n)) u2248 O(n), Space: O(n)
75Redundant ConnectionMediumUnion FindGoogleMicrosoftShow
IDEA: Add edges one by one with Union-Find. First edge connecting already-connected nodes = redundant. def findRedundantConnection(edges): parent = list(range(len(edges) + 1)) def find(x): while parent[x] != x: parent[x] = parent[parent[x]] x = parent[x] return x for a, b in edges: ra, rb = find(a), find(b) if ra == rb: return [a, b] parent[ra] = rb Time: O(n * u03b1(n)), Space: O(n)
76Accounts MergeMediumUnion Find + SortGoogleMetaShow
IDEA: Union-Find on emails. Map emails to owner. Union same-account emails. Group by root. def accountsMerge(accounts): parent = {}; emailToName = {} def find(x): while parent[x] != x: parent[x] = parent[parent[x]] x = parent[x] return x for acc in accounts: for email in acc[1:]: emailToName[email] = acc[0] if email not in parent: parent[email] = email parent[find(email)] = find(acc[1]) groups = {} for email in parent: root = find(email) groups.setdefault(root, []).append(email) return [[emailToName[r]] + sorted(e) for r, e in groups.items()] Time: O(n * u03b1(n)), Space: O(n)
🌳 Advanced Trees (7 problems)
#ProblemDifficultyPatternCompaniesSolution
77Diameter of Binary TreeEasyDFSGoogleMetaShow
IDEA: At each node, diameter = left_height + right_height. Track global max. Return height up. def diameterOfBinaryTree(root): res = [0] def dfs(node): if not node: return 0 l, r = dfs(node.left), dfs(node.right) res[0] = max(res[0], l + r) return 1 + max(l, r) dfs(root) return res[0] Time: O(n), Space: O(h)
78Same TreeEasyDFSAmazonMicrosoftShow
IDEA: Both null = equal. One null = not equal. Both exist = vals match AND subtrees match. def isSameTree(p, q): if not p and not q: return True if not p or not q: return False return (p.val == q.val and isSameTree(p.left, q.left) and isSameTree(p.right, q.right)) Time: O(n), Space: O(h)
79Subtree of Another TreeEasyDFSGoogleAmazonShow
IDEA: At each node in main tree, check if subtree rooted there is identical to subRoot. def isSameTree(p, q): if not p and not q: return True if not p or not q: return False return (p.val == q.val and isSameTree(p.left, q.left) and isSameTree(p.right, q.right)) def isSubtree(root, subRoot): if not root: return False if isSameTree(root, subRoot): return True return (isSubtree(root.left, subRoot) or isSubtree(root.right, subRoot)) Time: O(m*n), Space: O(h)
80Binary Tree Right Side ViewMediumBFSMicrosoftGoogleShow
IDEA: BFS level-by-level. The last node processed at each level is visible from right side. def rightSideView(root): if not root: return [] res, q = [], deque([root]) while q: for i in range(len(q)): node = q.popleft() if node.left: q.append(node.left) if node.right: q.append(node.right) res.append(node.val) return res Time: O(n), Space: O(n)
81Kth Smallest Element in BSTMediumInorderGoogleAmazonShow
IDEA: Inorder traversal of BST = sorted order. Do inorder iteratively, return k-th element. def kthSmallest(root, k): stack = []; curr = root while stack or curr: while curr: stack.append(curr) curr = curr.left curr = stack.pop() k -= 1 if k == 0: return curr.val curr = curr.right Time: O(H + k), Space: O(H)
82Construct Binary Tree from Preorder and InorderMediumRecursionGoogleMetaShow
IDEA: preorder[0] is root. Find it in inorder to split left/right. Use hashmap for O(1) index lookup. def buildTree(preorder, inorder): inorder_map = {v: i for i, v in enumerate(inorder)} idx = [0] # preorder index def build(lo, hi): if lo > hi: return None val = preorder[idx[0]] idx[0] += 1 root = TreeNode(val) mid = inorder_map[val] root.left = build(lo, mid - 1) root.right = build(mid + 1, hi) return root return build(0, len(inorder) - 1) Time: O(n), Space: O(n)
83Binary Tree Maximum Path SumHardDFSAmazonGoogleMicrosoftShow
IDEA: At each node, max path = val + max(left,0) + max(right,0). Return single-branch gain upward. def maxPathSum(root): res = [float('-inf')] def dfs(node): if not node: return 0 left = max(dfs(node.left), 0) right = max(dfs(node.right), 0) res[0] = max(res[0], node.val + left + right) return node.val + max(left, right) dfs(root) return res[0] Time: O(n), Space: O(h)
πŸ—ΊοΈ Advanced Graphs (5 problems)
#ProblemDifficultyPatternCompaniesSolution
84Pacific Atlantic Water FlowMediumMulti-source BFSGoogleAmazonShow
IDEA: DFS/BFS from ocean borders inward (reverse flow). Find cells reachable from BOTH oceans. def pacificAtlantic(heights): m, n = len(heights), len(heights[0]) pac, atl = set(), set() def dfs(r, c, visit, prevH): if (r<0 or c<0 or r>=m or c>=n or (r,c) in visit or heights[r][c] < prevH): return visit.add((r,c)) for dr,dc in [(1,0),(-1,0),(0,1),(0,-1)]: dfs(r+dr, c+dc, visit, heights[r][c]) for c in range(n): dfs(0,c,pac,0); dfs(m-1,c,atl,0) for r in range(m): dfs(r,0,pac,0); dfs(r,n-1,atl,0) return list(pac & atl) Time: O(m*n), Space: O(m*n)
85Surrounded RegionsMediumDFSGoogleMetaShow
IDEA: DFS from border O's, mark safe (T). Then flip remaining Ou2192X and Tu2192O. def solve(board): m, n = len(board), len(board[0]) def dfs(r, c): if r<0 or c<0 or r>=m or c>=n or board[r][c]!='O': return board[r][c] = 'T' for dr,dc in [(1,0),(-1,0),(0,1),(0,-1)]: dfs(r+dr, c+dc) for r in range(m): for c in range(n): if (r in (0,m-1) or c in (0,n-1)) and board[r][c]=='O': dfs(r,c) for r in range(m): for c in range(n): if board[r][c]=='O': board[r][c]='X' elif board[r][c]=='T': board[r][c]='O' Time: O(m*n), Space: O(m*n)
86Rotting OrangesMediumMulti-source BFSAmazonGoogleShow
IDEA: Multi-source BFS from all rotten oranges at once. Each BFS level = 1 minute. def orangesRotting(grid): m, n = len(grid), len(grid[0]) q = deque(); fresh = 0 for r in range(m): for c in range(n): if grid[r][c]==2: q.append((r,c)) elif grid[r][c]==1: fresh += 1 mins = 0 while q and fresh: mins += 1 for _ in range(len(q)): r,c = q.popleft() for dr,dc in [(1,0),(-1,0),(0,1),(0,-1)]: nr,nc = r+dr, c+dc if 0<=nr
87Graph Valid TreeMediumUnion Find / DFSGoogleMicrosoftShow
IDEA: Tree = (1) edges == n-1 AND (2) all connected. Use Union-Find to check both. def validTree(n, edges): if len(edges) != n - 1: return False parent = list(range(n)) def find(x): while parent[x] != x: parent[x] = parent[parent[x]] x = parent[x] return x for a, b in edges: ra, rb = find(a), find(b) if ra == rb: return False parent[ra] = rb return True Time: O(n * u03b1(n)), Space: O(n)
88Alien DictionaryHardTopological SortGoogleAmazonMetaShow
IDEA: Compare adjacent words for char ordering edges. Build graph, topological sort. Cycle = invalid. def alienOrder(words): graph = {c: set() for w in words for c in w} indeg = {c: 0 for c in graph} for i in range(len(words)-1): w1, w2 = words[i], words[i+1] ml = min(len(w1), len(w2)) if len(w1)>len(w2) and w1[:ml]==w2[:ml]: return "" for j in range(ml): if w1[j] != w2[j]: if w2[j] not in graph[w1[j]]: graph[w1[j]].add(w2[j]) indeg[w2[j]] += 1 break q = deque([c for c in indeg if indeg[c]==0]) res = [] while q: c = q.popleft(); res.append(c) for nei in graph[c]: indeg[nei] -= 1 if indeg[nei]==0: q.append(nei) return ''.join(res) if len(res)==len(graph) else "" Time: O(C), Space: O(C)
🎲 Advanced DP (7 problems)
#ProblemDifficultyPatternCompaniesSolution
89Decode WaysMedium1D DPAmazonGoogleMetaShow
IDEA: dp[i] = ways to decode. Single digit (1-9): add dp[i-1]. Two digits (10-26): add dp[i-2]. def numDecodings(s): if not s or s[0] == '0': return 0 prev2, prev1 = 1, 1 for i in range(1, len(s)): curr = 0 if s[i] != '0': curr += prev1 two = int(s[i-1:i+1]) if 10 <= two <= 26: curr += prev2 prev2, prev1 = prev1, curr return prev1 Time: O(n), Space: O(1)
90Unique PathsMedium2D DPGoogleAmazonShow
IDEA: dp[j] = paths to column j. Each cell = above + left. Space-optimized to 1D. def uniquePaths(m, n): dp = [1] * n for i in range(1, m): for j in range(1, n): dp[j] += dp[j-1] return dp[-1] Time: O(m*n), Space: O(n)
91Partition Equal Subset SumMedium0/1 KnapsackMicrosoftGoogleShow
IDEA: Find if subset sums to total/2. 0/1 knapsack with boolean DP. def canPartition(nums): total = sum(nums) if total % 2: return False target = total // 2 dp = {0} for n in nums: dp = dp | {s + n for s in dp if s + n <= target} return target in dp Time: O(n * sum/2), Space: O(sum/2)
92Target SumMediumDP / KnapsackGoogleAmazonShow
IDEA: Rephrase: find subset P where sum(P) = (total + target) / 2. Then subset sum DP. def findTargetSumWays(nums, target): total = sum(nums) if (total + target) % 2 or abs(target) > total: return 0 t = (total + target) // 2 dp = [0] * (t + 1) dp[0] = 1 for n in nums: for j in range(t, n - 1, -1): dp[j] += dp[j - n] return dp[t] Time: O(n * target), Space: O(target)
93Longest Palindromic SubsequenceMedium2D DPAmazonMicrosoftShow
IDEA: LPS(s) = LCS(s, reverse(s)). Standard 2D DP for Longest Common Subsequence. def longestPalindromeSubseq(s): n = len(s); rev = s[::-1] dp = [[0]*(n+1) for _ in range(n+1)] for i in range(1, n+1): for j in range(1, n+1): if s[i-1] == rev[j-1]: dp[i][j] = dp[i-1][j-1] + 1 else: dp[i][j] = max(dp[i-1][j], dp[i][j-1]) return dp[n][n] Time: O(nu00b2), Space: O(nu00b2)
94Interleaving StringMedium2D DPGoogleAmazonShow
IDEA: dp[i][j] = can s3[0..i+j] be formed by interleaving s1[0..i] and s2[0..j]? def isInterleave(s1, s2, s3): m, n = len(s1), len(s2) if m + n != len(s3): return False dp = [[False]*(n+1) for _ in range(m+1)] dp[0][0] = True for i in range(m+1): for j in range(n+1): if i>0 and s1[i-1]==s3[i+j-1]: dp[i][j] |= dp[i-1][j] if j>0 and s2[j-1]==s3[i+j-1]: dp[i][j] |= dp[i][j-1] return dp[m][n] Time: O(m*n), Space: O(m*n)
95Minimum Cost to Cut a StickHardInterval DPGoogleShow
IDEA: Sort cuts, add 0 and n as boundaries. dp[i][j] = min cost to cut between cuts[i]..cuts[j]. def minCost(n, cuts): cuts = sorted([0] + cuts + [n]) m = len(cuts) dp = [[0]*m for _ in range(m)] for length in range(2, m): for i in range(m - length): j = i + length dp[i][j] = float('inf') for k in range(i+1, j): dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j] + cuts[j] - cuts[i]) return dp[0][m-1] Time: O(cu00b3), Space: O(cu00b2)
πŸ—οΈ Design Problems (4 problems)
#ProblemDifficultyPatternCompaniesSolution
96Design TwitterMediumOOP + HeapAmazonGoogleShow
IDEA: HashMap for follows + tweets. News feed = merge latest from each followee using max-heap. import heapq class Twitter: def __init__(self): self.time = 0 self.tweets = {} # userId -> [(time, tweetId)] self.follows = {} # userId -> set(followeeIds) def postTweet(self, userId, tweetId): self.tweets.setdefault(userId, []).append( (self.time, tweetId)) self.time -= 1 # negative for max-heap def getNewsFeed(self, userId): self.follows.setdefault(userId, set()).add(userId) heap = [] for fid in self.follows[userId]: tw = self.tweets.get(fid, []) if tw: i = len(tw) - 1 heapq.heappush(heap, (tw[i][0], tw[i][1], fid, i)) res = [] while heap and len(res) < 10: t, tid, fid, i = heapq.heappop(heap) res.append(tid) if i > 0: i -= 1 tw = self.tweets[fid] heapq.heappush(heap, (tw[i][0], tw[i][1], fid, i)) return res Time: O(F * log F) for feed (F=followees), Space: O(tweets)
97Design Add and Search Words (Trie)MediumTrie + DFSGoogleMicrosoftShow
IDEA: Trie for insert. Search uses DFS β€” for '.', try all 26 children. Regular chars follow match. class WordDictionary: def __init__(self): self.root = {} def addWord(self, word): node = self.root for c in word: node = node.setdefault(c, {}) node['#'] = True def search(self, word): def dfs(node, i): if i == len(word): return '#' in node if word[i] == '.': return any(dfs(node[c], i+1) for c in node if c != '#') if word[i] not in node: return False return dfs(node[word[i]], i+1) return dfs(self.root, 0) Insert: O(m), Search: O(m) avg
98Serialize and Deserialize BSTMediumBST + PreorderMetaGoogleShow
IDEA: Preorder traversal to serialize (BST = no null markers needed). Deserialize with bounds. class Codec: def serialize(self, root): vals = [] def dfs(node): if node: vals.append(str(node.val)) dfs(node.left); dfs(node.right) dfs(root) return ' '.join(vals) def deserialize(self, data): if not data: return None q = deque(map(int, data.split())) def build(lo, hi): if not q or q[0] < lo or q[0] > hi: return None val = q.popleft() node = TreeNode(val) node.left = build(lo, val) node.right = build(val, hi) return node return build(float('-inf'), float('inf')) Time: O(n), Space: O(n)
99Design HashMapEasyDesignGoogleAmazonMicrosoftShow
IDEA: Array of buckets with linked-list chaining. Hash = key % size. Handle collisions. class MyHashMap: def __init__(self): self.size = 1000 self.buckets = [[] for _ in range(self.size)] def put(self, key, val): bucket = self.buckets[key % self.size] for i, (k, v) in enumerate(bucket): if k == key: bucket[i] = (key, val); return bucket.append((key, val)) def get(self, key): for k, v in self.buckets[key % self.size]: if k == key: return v return -1 def remove(self, key): bucket = self.buckets[key % self.size] for i, (k, v) in enumerate(bucket): if k == key: bucket.pop(i); return Time: O(1) avg, O(n/k) worst