DSA Concepts, Code Examples & Practice
1. Arrays & Strings
Concepts: Traversing, insertion, deletion, sliding window, two pointers, prefix sum
def twoSum(nums, target):
hashmap = {}
for i, num in enumerate(nums):
if target - num in hashmap:
return [hashmap[target - num], i]
hashmap[num] = i
Practice:
Two Sum: https://leetcode.com/problems/two-sum/
Maximum Subarray: https://leetcode.com/problems/maximum-subarray/
Longest Substring Without Repeating Characters:
https://leetcode.com/problems/longest-substring-without-repeating-characters/
2. Linked Lists
Concepts: Singly vs doubly, fast & slow pointer, reversing a list
def reverseList(head):
prev = None
while head:
next_node = head.next
head.next = prev
prev = head
head = next_node
return prev
Practice:
Reverse Linked List: https://leetcode.com/problems/reverse-linked-list/
Linked List Cycle: https://leetcode.com/problems/linked-list-cycle/
3. Stacks & Queues
Concepts: Stack (LIFO), Queue (FIFO), Monotonic Stack, Min Stack
def isValid(s):
stack = []
mapping = {')': '(', ']': '[', '}': '{'}
for char in s:
if char in mapping:
top = stack.pop() if stack else '#'
if mapping[char] != top:
return False
else:
stack.append(char)
return not stack
Practice:
Valid Parentheses: https://leetcode.com/problems/valid-parentheses/
Min Stack: https://leetcode.com/problems/min-stack/
4. Trees & Binary Search Trees
Concepts: DFS (Inorder, Preorder, Postorder), BFS, Height, Balance, Diameter
def inorderTraversal(root):
return inorderTraversal(root.left) + [root.val] + inorderTraversal(root.right) if
root else []
Practice:
Max Depth of Binary Tree: https://leetcode.com/problems/maximum-depth-of-binary-tree/
Symmetric Tree: https://leetcode.com/problems/symmetric-tree/
5. Binary Search
Concepts: Classic binary search, lower/upper bounds, binary search on answer
def binarySearch(arr, target):
low, high = 0, len(arr)-1
while low <= high:
mid = (low + high)//2
if arr[mid] == target:
return mid
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
Practice:
Binary Search: https://leetcode.com/problems/binary-search/
Search in Rotated Sorted Array: https://leetcode.com/problems/search-in-rotated-sorted-array/
6. Recursion & Backtracking
Concepts: Base case, recursive calls, permutations, subsets, N-Queens
def subsets(nums):
res = []
def backtrack(start, path):
res.append(path)
for i in range(start, len(nums)):
backtrack(i+1, path + [nums[i]])
backtrack(0, [])
return res
Practice:
Subsets: https://leetcode.com/problems/subsets/
Permutations: https://leetcode.com/problems/permutations/
7. Dynamic Programming
Concepts: Memoization (Top-Down), Tabulation (Bottom-Up), Knapsack, LCS
def fib(n, memo={}):
if n <= 1:
return n
if n not in memo:
memo[n] = fib(n-1, memo) + fib(n-2, memo)
return memo[n]
Practice:
Climbing Stairs: https://leetcode.com/problems/climbing-stairs/
House Robber: https://leetcode.com/problems/house-robber/
Longest Increasing Subsequence: https://leetcode.com/problems/longest-increasing-subsequence/
8. Greedy Algorithms
Concepts: Always choose the locally optimal solution
def canJump(nums):
goal = len(nums) - 1
for i in range(len(nums)-2, -1, -1):
if i + nums[i] >= goal:
goal = i
return goal == 0
Practice:
Jump Game: https://leetcode.com/problems/jump-game/
Best Time to Buy and Sell Stock II: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
9. Graphs
Concepts: DFS, BFS, Topological Sort, Shortest Path, MST
def dfs(graph, node, visited):
if node not in visited:
visited.add(node)
for neighbor in graph[node]:
dfs(graph, neighbor, visited)
Practice:
Number of Islands: https://leetcode.com/problems/number-of-islands/
Course Schedule: https://leetcode.com/problems/course-schedule/
10. Tries, Heaps, and Advanced Topics
Concepts: Trie, Heap (Min/Max), Segment Tree, Union Find
Practice:
Implement Trie: https://leetcode.com/problems/implement-trie-prefix-tree/
Find Median from Data Stream: https://leetcode.com/problems/find-median-from-data-stream/
Longest Consecutive Sequence: https://leetcode.com/problems/longest-consecutive-sequence/