Courses / Intermediate
Data Structures & Algorithms
Big-O, arrays, hash maps, trees, graphs and dynamic programming, with 120 interview-style problems and hints.
9 lessons16 hours176k learnersCertificate with Pro
Code you'll write
def binary_search(items, target):
lo, hi = 0, len(items) - 1
while lo <= hi:
mid = (lo + hi) // 2
if items[mid] == target:
return mid
if items[mid] < target:
lo = mid + 1
else:
hi = mid - 1
return -1
from collections import deque
def shortest_path(graph, start, goal):
queue = deque([[start]])
seen = {start}
while queue:
path = queue.popleft()
if path[-1] == goal:
return path
for nxt in graph[path[-1]]:
if nxt not in seen:
seen.add(nxt)
queue.append(path + [nxt])
return None