Docsity

DSA EXAM CHEAT SHEET, Cheat Sheet of Data Structures and Algorithms

Toggle sidebar
bg1
$๎˜‚cat๎˜‚./exam-prep/dsa_cheatsheet.md
QUICK๎˜‚REFERENCE๎˜‚ยท๎˜‚INTERVIEW๎˜‚&๎˜‚EXAM๎˜‚PREP
Data๎˜„Structures
&๎˜„Algorithms
A๎˜‚scannable,๎˜‚exam-ready๎˜‚reference:๎˜‚Big-O๎˜‚tables,
core๎˜‚data๎˜‚structures,๎˜‚and๎˜‚the๎˜‚five๎˜‚algorithmic
patterns๎˜‚that๎˜‚solve๎˜‚most๎˜‚coding๎˜‚problems๎˜‚โ€”๎˜‚with
clean,๎˜‚commented๎˜‚Python.
Big-O๎˜‚Tables ๎˜‚Code๎˜‚Templates ๎˜‚Pattern๎˜‚Recognition ๎˜‚Bit๎˜‚Tricks
{๎˜‚}
Edition๎˜‚01๎˜‚ยท๎˜‚7๎˜‚pages๎˜‚ยท๎˜‚Python
pf3
pf4
pf5
pf8

Partial preview of the text

Download DSA EXAM CHEAT SHEET and more Cheat Sheet Data Structures and Algorithms in PDF only on Docsity!

$ cat ./exam-prep/dsa_cheatsheet.md

Q U I C K R E F E R E N C E ยท I N T E R V I E W & E X A M P R E P

Data Structures

& Algorithms

A scannable, exam-ready reference: Big-O tables, core data structures, and the five algorithmic patterns that solve most coding problems โ€” with clean, commented Python. Big-O Tables Code Templates Pattern Recognition Bit Tricks

Edition 01 ยท 7 pages ยท Python

pg. 03

pg. 04

pg. 05

pg. 06

pg. 07

contents.md

/ / T A B L E O F C O N T E N T S

What's Inside

Four sections, zero fluff โ€” built for a last-minute scan before the exam.

01 Big-O Complexity Summary Access, search, insert, delete & space โ€” for every core structure and sort. 02 Data Structures Essentials Linked lists, binary trees & BSTs, graphs โ€” definitions, traversals, code. 03 Algorithmic Patterns Two Pointers, Sliding Window, Binary Search โ€” ready-to-reuse templates. 04 Bit Manipulation Tricks Even/odd checks, power-of-2, XOR cancellation โ€” the classic one-liners. 05 Pattern Recognition Speed-Run "If you see X, think Y" โ€” a lookup table for the exam room.

How to use this guide โ€” Read top to bottom once, then use it as a lookup: when a problem statement matches a

trigger phrase in Section 5, jump straight to the matching template.

contents.md 01_big_o.md 02_structures.py

Data Structures Essentials

Definitions, traversal orders, and minimal reference implementations.

## Linked List (Singly)

Node = data + pointer to next node

Pros: dynamic size, O(1) insert/delete at a known position

Cons: no random access, extra memory for pointers

linked_list.py class ListNode: def init(self, val= 0 , next= None ): self.val = val # node's data self.next = next # pointer to next node ## Binary Tree & BST

Binary Tree โ€” each node has โ‰ค 2 children ( left , right )

BST rule โ€” left subtree < root < right subtree

Traversal Order Use Case

In-Order Left โ†’ Root โ†’ Right Sorted output from a BST

Pre-Order Root โ†’ Left โ†’ Right Cloning a tree

Post-Order Left โ†’ Right โ†’ Root Deleting a tree / bottom-up math

tree_node.py class TreeNode: def init(self, val= 0 , left= None , right= None ): self.val = val self.left = left self.right = right ## Graphs Representation Space Edge Lookup Best For

Adjacency Matrix O(Vยฒ) O(1) Dense graphs

Adjacency List O(V + E) O(V) Sparse graphs

Cycle detection (directed) โ€” DFS coloring: white = unvisited, gray = visiting, black = done

Cycle detection (alt) โ€” Kahn's Algorithm (topological sort)

02_structures.py 03_patterns.py

Algorithmic Patterns

Ready-to-reuse templates โ€” recognize the trigger, plug in the code.

## Two Pointers

Trigger words: sorted array, pair sum, triplet

two_pointers.py def two_pointer_template(arr, target): left, right = 0 , len(arr) - 1 while left < right: current_sum = arr[left] + arr[right] if current_sum == target: return [left, right] elif current_sum < target: left += 1 # need bigger sum else : right -= 1 # need smaller sum return [- 1 , - 1 ] ## Sliding Window

Trigger words: subarray, contiguous, "longest / shortest substring"

sliding_window.py def sliding_window_template(arr): left = 0 max_len = 0 window_state = {} for right in range(len(arr)): # 1. expand: add arr[right] to window_state while window_state_invalid(): # 2. shrink while condition broken left += 1 max_len = max(max_len, right - left + 1 ) # 3. update answer return max_len

04_bitwise.py 05_speed_run.md

Pattern Recognition Speed-Run

The exam-room lookup: match the phrasing, pick the pattern.

If you seeโ€ฆ Thinkโ€ฆ

"sorted array" + pair / triplet Two Pointers

"longest / shortest subarray or substring" Sliding Window

"find X in sorted / monotonic space" Binary Search

"connected components / paths" Graph DFS / BFS

"top K" / "kth largest" Heap

"duplicate detection, O(1) lookup" Hash Table

"recursive tree comparison" Tree Traversal

Last-minute rule โ€” When stuck, restate the problem in one of the seven trigger phrases above. If none fit, it's probably

a variation of Two Pointers or Sliding Window in disguise.

/ / e n d _ o f _ f i l e

Good luck in there.

Keep this guide open in a second tab, work top-down through Section 5

when you're stuck, and trust the patterns โ€” they cover the overwhelming

majority of exam and interview questions.

V I R E L E X D E S I G N S

Study Guides & Visual Systems