Glenpl

Untitled

Feb 9th, 2026
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.71 KB | None | 0 0
  1. ### Part 1: The Google Interview Cheat Sheet (First 5 Mins)
  2. '''
  3. If you get stuck, follow these steps:
  4.  
  5. 1.  **Clarify (3 min):** Ask about input size (for complexity), data types (negative numbers? duplicates?), and empty/null cases.
  6. 2.  **Brute Force (5 min):** State the obvious $O(N^2)$ or $O(2^n)$ solution aloud. Do NOT code it unless they ask.
  7. 3.  **Optimize (10 min):** Look for:
  8.    *   **Bottlenecks:** Can a Hash Map reduce $O(N^2)$ to $O(N)$?
  9.    *   **Unnecessary Work:** Can you use Two Pointers or Sliding Window?
  10.    *   **Sorted Data:** If it's sorted, think **Binary Search**.
  11. 4.  **Dry Run (5 min):** Before coding, trace your logic with a small example on the whiteboard/doc.
  12. 5.  **Code (15 min):** Focus on clean Pythonic code.
  13. 6.  **Analyze (2 min):** State Time and Space complexity immediately.
  14. '''
  15. # ------------------------------------------------------------------------------
  16.  
  17. def bisect_left(arr, target):
  18.     left, right = 0, len(arr)
  19.     while left < right:
  20.         mid = (left + right) // 2
  21.         if arr[mid] < target:  # <= for bisect_right
  22.             left = mid + 1
  23.         else:
  24.             right = mid
  25.     return left
  26. # --- top K
  27. import heapq
  28. nums = [10, 2, 5, 1, 9]
  29. heapq.heapify(nums) # Transforms list into a heap in O(N)
  30. smallest = heapq.heappop(nums) # Returns 1 (O(log N))
  31. # --- BFS - Shortest Path
  32. def bfs(graph, start):
  33.     visited = {start}
  34.     queue = deque([start])
  35.     while queue:
  36.         node = queue.popleft()
  37.         print(node)
  38.         for neighbor in graph[node]:
  39.             if neighbor not in visited:
  40.                 visited.add(neighbor)
  41.                 queue.append(neighbor)
  42. # --- dijkstra - shortest path
  43. import heapq
  44. def dijkstra(adj, start):
  45.     pq = [(0, start)] # (distance, node)
  46.     dist = {node: float('inf') for node in adj}
  47.     dist[start] = 0
  48.     while pq:
  49.         d, u = heapq.heappop(pq)
  50.         if d > dist[u]: continue
  51.         for v, weight in adj[u]:
  52.             if dist[u] + weight < dist[v]:
  53.                 dist[v] = dist[u] + weight
  54.                 heapq.heappush(pq, (dist[v], v))
  55. # Time: O(E log V), Space: O(V + E)
  56. # --- DFS - Connectivity/Pathfinding
  57. def dfs(graph, node, visited=None):
  58.     if visited is None: visited = set()
  59.     visited.add(node)
  60.     print(node)
  61.     for neighbor in graph[node]:
  62.         if neighbor not in visited:
  63.             dfs(graph, neighbor, visited)
  64. # --- DP - Longest Common Subsequence (LCS)
  65. # Problem: s1="abcde", s2="ace". Result: "ace" (3).
  66. # Logic: If s1[i] == s2[j], 1 + dp[i-1][j-1]. Else, max(dp[i-1][j], dp[i][j-1]).
  67. dp[i][j] = (1 + dp[i-1][j-1] if s1[i] == s2[j] else max(dp[i-1][j], dp[i][j-1]))
  68. # --- Tree traversals O(n)
  69. '''
  70. In-order:
  71. - is a Binary Search Tree (BST)? If you do an in-order traversal and the numbers aren't sorted, it's not a BST.
  72. Pre-order:
  73. - Creating a copy of a tree. If you insert nodes into a new tree in pre-order, you get an exact clone.
  74. - directory structures
  75. Post-order:
  76. - Deleting a tree (you delete children before the parent).
  77. - Calculating Tree Height or Diameter. You need the info from both children (bottom-up) before you can calculate the result for the current node.
  78. '''
  79. # ---
  80. '''
  81. 1.  **Don't memorize code.** Memorize the **trigger**:
  82.    *   "Shortest path in a grid?" -> **BFS**.
  83.    *   "Sorted array?" -> **Binary Search** or **Two Pointers**.
  84.    *   "Need to find duplicates/pairs?" -> **Hash Map (Dict)**.
  85.    *   "Top K elements?" -> **Heap**.
  86.    *   "Subsequences/Choices?" -> **Recursion or DP**.
  87. 2.  **Clean Code:** Use descriptive names (`queue`, `visited`, `distance`). Google cares about readability.
  88. 3.  **Check Constraints:** If the input $N$ is $10^5$, an $O(N^2)$ solution will fail. You need $O(N \log N)$ or $O(N)$.
  89. '''
  90.  
Advertisement
Add Comment
Please, Sign In to add comment