0% found this document useful (0 votes)
12 views7 pages

Algorithm Complexity Overview

The document outlines various coding algorithms, their time and space complexities, and provides Python code examples for each. It covers core algorithm families including sorting, searching, recursion, greedy algorithms, dynamic programming, graph algorithms, and more. Additionally, it offers strategies for interview preparation and problem-solving techniques.

Uploaded by

rdxsparrowgaming
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views7 pages

Algorithm Complexity Overview

The document outlines various coding algorithms, their time and space complexities, and provides Python code examples for each. It covers core algorithm families including sorting, searching, recursion, greedy algorithms, dynamic programming, graph algorithms, and more. Additionally, it offers strategies for interview preparation and problem-solving techniques.

Uploaded by

rdxsparrowgaming
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

all coding algm and its time , space complaxity , working ,

python code

This is not something you “memorize as one list” — it’s a mental map of patterns. I’ll give
you a clean, structured system so you can actually use it in interviews + coding rounds ⚡

🧠 1. Core Algorithm Families (with complexity +


intuition)
🔹 1. Sorting Algorithms
Algorithm Time Complexity Space Key Idea

Bubble Sort O(n²) O(1) Swap adjacent repeatedly

Selection Sort O(n²) O(1) Pick min and place

Insertion Sort O(n²) / O(n best) O(1) Insert into sorted part

Merge Sort O(n log n) O(n) Divide & merge

Quick Sort O(n log n) avg / O(n²) O(log n) Pivot partition


worst

Heap Sort O(n log n) O(1) Heap structure

🔥 Python Example – Quick Sort

def quick_sort(arr):
if len(arr) <= 1:
return arr

pivot = arr[len(arr)//2]
left = [x for x in arr if x < pivot]
mid = [x for x in arr if x == pivot]
right= [x for x in arr if x > pivot]

return quick_sort(left) + mid + quick_sort(right)


🔹 2. Searching Algorithms
Algorithm Time Space Idea

Linear Search O(n) O(1) Check one by one

Binary Search O(log n) O(1) Divide sorted array

🔥 Binary Search

def binary_search(arr, target):


l, r = 0, len(arr)-1

while l <= r:
mid = (l + r)//2
if arr[mid] == target:
return mid
elif arr[mid] < target:
l = mid + 1
else:
r = mid - 1
return -1

🔹 3. Recursion & Backtracking


Concept Time Idea

Recursion varies function calls itself

Backtracking exponential try → undo → try

🔥 Example: Subsets

def subsets(nums):
res = []

def backtrack(start, path):


[Link](path[:])
for i in range(start, len(nums)):
[Link](nums[i])
backtrack(i+1, path)
[Link]()

backtrack(0, [])
return res
🔹 4. Divide & Conquer
Algo Time Idea

Merge Sort O(n log n) split + merge

Quick Sort O(n log n) partition

Binary Search O(log n) halve

🔹 5. Greedy Algorithms
Problem Idea

Activity Selection pick earliest finish

Huffman Coding optimal merge

Kruskal / Prim MST

🔥 Example: Activity Selection

def activity_selection(start, end):


activities = sorted(zip(start, end), key=lambda x: x[1])

res = [activities[0]]
last_end = activities[0][1]

for s, e in activities[1:]:
if s >= last_end:
[Link]((s, e))
last_end = e

return res

🔹 6. Dynamic Programming (DP)


Type Idea

Memoization recursion + cache

Tabulation bottom-up
Common Problems:
Fibonacci → O(n)
Knapsack → O(nW)
Longest Common Subsequence → O(n²)

🔥 Fibonacci DP

def fib(n):
dp = [0]*(n+1)
dp[1] = 1

for i in range(2, n+1):


dp[i] = dp[i-1] + dp[i-2]

return dp[n]

🔹 7. Graph Algorithms
Algorithm Time Use

BFS O(V+E) shortest path (unweighted)

DFS O(V+E) traversal

Dijkstra O(E log V) shortest path

Floyd Warshall O(V³) all pairs

Kruskal O(E log E) MST

Prim O(E log V) MST

🔥 BFS

from collections import deque

def bfs(graph, start):


visited = set()
q = deque([start])

while q:
node = [Link]()
if node not in visited:
print(node)
[Link](node)
[Link](graph[node])

🔹 8. Sliding Window
Time O(n)

Idea shrink & expand window

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

🔹 9. Two Pointer
Time O(n)

Use sorted arrays

def two_sum_sorted(arr, target):


l, r = 0, len(arr)-1

while l < r:
s = arr[l] + arr[r]
if s == target:
return (l, r)
elif s < target:
l += 1
else:
r -= 1

🔹 10. Bit Manipulation


Trick Use

x & (x-1) remove last set bit

x^x=0 duplicates

def count_set_bits(n):
count = 0
while n:
n &= (n-1)
count += 1
return count

🔹 11. Heap / Priority Queue


Operation Time

Insert O(log n)

Extract O(log n)

import heapq

heap = []
[Link](heap, 10)
[Link](heap, 5)
print([Link](heap)) # 5

🔹 12. Trie (Prefix Tree)


Operation Time

Insert O(L)

Search O(L)

🔹 13. Union Find (DSU)


Operation Time

Find O(α(n))

Union O(α(n))

⚡ Real Interview Strategy (this is gold)


Instead of remembering all:

🧩 Pattern Mapping
Array + sum → Sliding Window / Two Pointer
Optimization → DP
Connectivity → Graph / Union-Find
All combinations → Backtracking
Sorted + search → Binary Search
Greedy choice → Greedy

🚀 If you want next level


I can build you:
🔥 100 MUST-DO problems mapped to each algorithm
⚡ FAANG-level cheat sheet (1-page memory map)
🧠 Brain training plan to think in < 30 sec per problem

Just say: “level up plan” and I’ll design it.

You might also like