Skip to main content

Posts

Showing posts from March, 2025

Common Algorithms

  1. Sorting Algorithms Algorithm Time Complexity (Best / Avg / Worst) Space Complexity Stable? When to Use Bubble Sort O(n) / O(n²) / O(n²) O(1) ✅ Yes Simple, but inefficient for large datasets Selection Sort O(n²) / O(n²) / O(n²) O(1) ❌ No When swapping is costly, but still inefficient Insertion Sort O(n) / O(n²) / O(n²) O(1) ✅ Yes Works well for nearly sorted arrays Merge Sort O(n log n) / O(n log n) / O(n log n) O(n) ✅ Yes Large datasets, stable sorting Quick Sort O(n log n) / O(n log n) / O(n²) O(log n) ❌ No Fastest for general use, but unstable Heap Sort O(n log n) / O(n log n) / O(n log n) O(1) ❌ No Best when you n...

Common coding patterns and techniques

  1. Hashmaps (Dictionaries in Python) When to Use: Fast lookups & insertions → O(1) average time complexity. Counting frequencies → e.g., character counts, word frequencies. Finding duplicates in O(n) time. Grouping items by common properties (like anagrams). Mapping relationships (e.g., parent-child, graph adjacency lists). Examples: Two Sum (Find two numbers that sum to a target) Group Anagrams Longest Substring Without Repeating Characters 2. Sliding Window When to Use: Subarrays or substrings with constraints. Finding max/min/k-distinct elements in a subarray. Fixed or dynamic window size problems. Examples: Longest Substring Without Repeating Characters Maximum Sum Subarray of Size K Smallest Subarray with a Given Sum Key Trick : Use a set or hashmap to track seen characters. 3. Two Pointers When to Use: Sorted arrays or linked...