Back to Course
Printable Cheat Sheet
ToolMate Technical Interview Revision Deck • Data Structures & Algorithms using JavaScriptUpdated 2026
Data Structures & Algorithms Complexity & Pattern Cheat Sheet
Time/Space complexities, algorithmic archetypes, pointer techniques & interview templates.
Top Interview Golden Rules & Senior Principles
- Always clarify input constraints: Can array be empty? Are values sorted? Can there be duplicates? Can numbers be negative?
- Sorting first takes O(n log n) but frequently reduces O(n²) brute-force problems to O(n) two-pointer solutions.
- Hash Map tradeoffs: trade O(n) space to achieve O(1) instantaneous lookup.
- Sliding Window pattern applies to contiguous sub-arrays/strings; Two Pointers apply to sorted pairs or cycle detection (Floyd's algorithm).
- Breadth-First Search (Queue) guarantees the shortest path in unweighted graphs; DFS (Stack/Recursion) explores topological or tree branches.
Time & Space Complexity Reference Matrix
| Data Structure / Algorithm | Best Time | Average Time | Worst Time | Space |
|---|---|---|---|---|
| Array (Index Access / Lookup) | O(1) | O(1) | O(1) | O(n) |
| Array (Insert / Delete at End) | O(1) | O(1) | O(n) | O(n) |
| Array (Insert / Delete at Beginning) | O(n) | O(n) | O(n) | O(n) |
| Hash Map / Set (Insert / Search / Delete) | O(1) | O(1) | O(n) | O(n) |
| Binary Search Tree (Balanced) | O(1) | O(log n) | O(log n) | O(n) |
| Merge Sort / Heap Sort | O(n log n) | O(n log n) | O(n log n) | O(n) |
| Quick Sort | O(n log n) | O(n log n) | O(n²) | O(log n) |
Top 4 Must-Know Algorithmic Patterns
Two Pointers (Opposite Ends)
while (left < right) { if (sum === target) return; sum < target ? left++ : right--; }Used on sorted arrays for target pairs, palindrome verification, or container with most water.
Fast & Slow Pointers (Floyd's Cycle)
slow = slow.next; fast = fast.next.next;
Detects cycles in linked lists in O(n) time and O(1) space, and finds linked list middle nodes.
Sliding Window
for (let r = 0; r < n; r++) { add(r); while (invalid) remove(l++); }Tracks longest/shortest sub-array matching criteria (e.g., longest substring without repeating characters).
Binary Search Template
mid = left + Math.floor((right - left) / 2)
Avoids integer overflow. In JS, bitwise `(left + right) >> 1` also works for 32-bit integers.
ToolMate Interview Prep • https://interview.toolmate.co.in • Free for personal study & interview preparation