Advanced
Graphs: BFS & DFS
A graph is a set of nodes connected by edges — modeling anything from social networks to road maps. Breadth-First Search (BFS) explores level by level using a queue, and is ideal for shortest paths in unweighted graphs. Depth-First Search (DFS) explores as far as possible down one path before backtracking, typically using a stack or recursion.
from collections import deque
def bfs(graph, start):
visited = {start}
queue = deque([start])
order = []
while queue:
node = queue.popleft()
order.append(node)
for neighbor in graph[node]:
if neighbor not in visited:
visited.add(neighbor)
queue.append(neighbor)
return order
Advanced
Hash Tables
A hash table maps keys to values using a hash function to compute an array index, giving average O(1) insert, lookup, and delete. Python's dict and JavaScript's Object/Map are both hash tables under the hood. Collisions (two keys hashing to the same slot) are handled internally via chaining or probing.