Master Algorithms Code with 422 free flashcards. Study using spaced repetition and focus mode for effective learning in Programming.
Sample card
What is Binary Search and what is its time complexity?
Write the implementation in Python.
Binary Search finds a target in a sorted array by repeatedly halving the search space.
Time: O(log n) | Space: O(1)def binary_search(arr, target):
lo, hi = 0, len(arr) - 1
while lo
Q · 1 of 422
What is Binary Search and what is its time complexity?
Write the implementation in Python.
Q · 2 of 422
What is Bubble Sort and what is its time complexity?
Write the implementation in Python.
Q · 3 of 422
What is Merge Sort and what is its time complexity?
Write the implementation in Python.
Binary Search finds a target in a sorted array by repeatedly halving the search space.
Time: O(log n) | Space: O(1)
def binary_search(arr, target):
lo, hi = 0, len(arr) - 1
while lo
Bubble Sort repeatedly swaps adjacent elements if they are in the wrong order.
Time: O(n²) | Space: O(1)
def bubble_sort(arr):
n = len(arr)
for i in range(n):
swapped = False
for j in range(0, n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
swapped = True
if not swapped:
break
return arr
Merge Sort is a divide-and-conquer algorithm that splits the array in half, recursively sorts each half, then merges them.
Time: O(n log n) | Space: O(n)
def merge_sort(arr):
if len(arr)
Quick Sort picks a pivot, partitions elements around it, then recursively sorts each partition.
Time: O(n log n) avg, O(n²) worst | Space: O(log n)
def quick_sort(arr, lo=0, hi=None):
if hi is None:
hi = len(arr) - 1
if lo < hi:
pivot_idx = partition(arr, lo, hi)
quick_sort(arr, lo, pivot_idx - 1)
quick_sort(arr, pivot_idx + 1, hi)
def partition(arr, lo, hi):
pivot = arr[hi]
i = lo - 1
for j in range(lo, hi):
if arr[j]
BFS explores a graph level by level using a queue. Used for shortest path in unweighted graphs.
Time: O(V + E) | Space: O(V)
from collections import deque
def bfs(graph, start):
visited = set([start])
queue = deque([start])
result = []
while queue:
node = queue.popleft()
result.append(node)
for neighbor in graph[node]:
if neighbor not in visited:
visited.add(neighbor)
queue.append(neighbor)
return result
DFS explores a graph by going as deep as possible before backtracking.
Time: O(V + E) | Space: O(V)
Iterative:
def dfs_iterative(graph, start):
visited = set()
stack = [start]
result = []
while stack:
node = stack.pop()
if node not in visited:
visited.add(node)
result.append(node)
for neighbor in graph[node]:
stack.append(neighbor)
return resultdef dfs_recursive(graph, node, visited=None):
if visited is None:
visited = set()
visited.add(node)
for neighbor in graph[node]:
if neighbor not in visited:
dfs_recursive(graph, neighbor, visited)
return visited
Dijkstra's finds the shortest path from a source to all vertices in a weighted graph with non-negative weights.
Time: O((V + E) log V) with min-heap | Space: O(V)
import heapq
def dijkstra(graph, start):
dist = {node: float('inf') for node in graph}
dist[start] = 0
heap = [(0, start)]
while heap:
d, u = heapq.heappop(heap)
if d > dist[u]:
continue
for v, weight in graph[u]:
if dist[u] + weight < dist[v]:
dist[v] = dist[u] + weight
heapq.heappush(heap, (dist[v], v))
return dist
Two Pointers uses two indices moving toward each other (or in the same direction) to solve problems on sorted arrays or linked lists in O(n).
Time: O(n) | Space: O(1)
def two_sum_sorted(arr, target):
lo, hi = 0, len(arr) - 1
while lo < hi:
s = arr[lo] + arr[hi]
if s == target:
return (lo, hi)
elif s < target:
lo += 1
else:
hi -= 1
return None
Sliding Window maintains a window of elements and slides it across the array, adding/removing one element at a time.
Time: O(n) | Space: O(1)
def max_sum_subarray(arr, k):
window_sum = sum(arr[:k])
max_sum = window_sum
for i in range(k, len(arr)):
window_sum += arr[i] - arr[i - k]
max_sum = max(max_sum, window_sum)
return max_sum
DP solves problems by breaking them into overlapping subproblems and storing results to avoid recomputation.
Top-down (Memoization): O(n) time, O(n) space
def fib_memo(n, memo={}):
if n
Kadane's Algorithm finds the maximum sum contiguous subarray in O(n) time.
Time: O(n) | Space: O(1)
def max_subarray(arr):
max_ending_here = arr[0]
max_so_far = arr[0]
for num in arr[1:]:
max_ending_here = max(num, max_ending_here + num)
max_so_far = max(max_so_far, max_ending_here)
return max_so_far
Given items with weights and values and a capacity W, find the maximum value without exceeding W. Each item is used at most once.
Time: O(n * W) | Space: O(n * W)
def knapsack(weights, values, W):
n = len(weights)
dp = [[0] * (W + 1) for _ in range(n + 1)]
for i in range(1, n + 1):
for w in range(W + 1):
dp[i][w] = dp[i-1][w]
if weights[i-1]
Flashcards
Flip to reveal
Focus Mode
Spaced repetition
Multiple Choice
Test your knowledge
Type Answer
Active recall
Learn Mode
Multi-round mastery
Match Game
Memory challenge