Welcome to the first week of my 8-week Data Structures and Algorithms (DSA) journey!
If you are just starting with DSA, the sheer number of problems out there can feel overwhelming. That is why I am spending these eight weeks focusing on patterns rather than just randomly grinding problems. For Week 1, I dove headfirst into Python data structures, specifically focusing on Arrays, Hash Maps, and the Two Pointer technique.
Here is a breakdown of the 5 problems I tackled this week, the patterns I used, and my biggest takeaways.
🧠Pattern 1: Hash Maps & Frequency Counting
Hash maps (or dictionaries in Python) are absolute lifesavers when it comes to optimizing time complexity. By trading a little bit of space, we can bring O(n²) operations down to O(n).
LeetCode #1: Two Sum
The classic. Instead of using a nested loop to check every pair, I used a dictionary to store the numbers I had already seen along with their indices. If the target - current_number was already in the dictionary, I had my answer instantly!
LeetCode #242: Valid Anagram
This problem is a great way to understand frequency counters. By mapping out the character counts of the first string, I could simply decrement the counts using the second string to check for a perfect match.
LeetCode #49: Group Anagrams
Taking anagrams a step further! I learned how to use a sorted tuple of the string (or a character count array) as a dictionary key to group matching anagrams together.
🎯 Pattern 2: The Two Pointer Technique
When dealing with sorted arrays or modifying arrays "in-place" without using extra memory, the two-pointer technique is incredibly efficient.
LeetCode #27: Remove Element
A great introduction to managing an array in-place. By keeping a "reader" pointer and a "writer" pointer, I could overwrite the elements I wanted to remove without creating a whole new array.
LeetCode #26: Remove Duplicates from Sorted Array
Similar to problem 27, but relying on the fact that the array is already sorted. The slow pointer keeps track of the unique elements, while the fast pointer scans ahead to skip the duplicates.
💡 Key Takeaways from Week 1
Python Dictionaries are OP: Mastering dict methods like .get() and using collections.defaultdict makes writing Pythonic DSA code so much cleaner.
In-Place Operations Save Memory: Modifying an array using two pointers is tricky to visualize at first, but it is essential for keeping space complexity at O(1).
Pattern Recognition Over Memorization: Understanding why a Hash Map works for Two Sum is vastly more important than memorizing the code for it.
Week 1 is in the books! I am building a lot of momentum and looking forward to diving deeper into new patterns next week.
Have you tackled any of these problems? What pattern was the hardest for you to grasp when you first started? Let me know in the comments!
Top comments (0)