DEV Community

Somuya Khandelwal
Somuya Khandelwal

Posted on

DAY 20 Closing the Week with Hashmap Challenges

Hello Readers!

Today marked the final day of Week 4 in my competitive programming journey, and I decided to spice things up with Hashmap Problems. Instead of my usual step-by-step breakdown, I want to share how today felt like solving puzzles where every piece needed to fit perfectly. Hashmaps are like my go-to toolbox now—they’re efficient, versatile, and, when used right, they make solving problems feel incredibly rewarding.


Today’s Puzzles

  1. Contains Duplicate II (Medium Difficulty)

    • This problem felt like spotting patterns in a crowd. The task was to determine if there are duplicates in an array within a given range k.
    • The Aha Moment:
      • Using a hashmap to store the last-seen index of each number made everything click. As I slid through the array, I just checked if the current index and the stored index were within range.
    • What Made It Fun:
      • It wasn’t just about finding duplicates—it was about tracking them efficiently and understanding how proximity impacts the solution.
  2. Longest Consecutive Sequence (Medium Difficulty)

    • Imagine trying to build the longest streak in a game but with numbers scattered everywhere. This problem was about finding the longest sequence of consecutive numbers in an unsorted array.
    • The Aha Moment:
      • Instead of sorting, I used a hashmap to track the boundaries of sequences dynamically. The clever part was merging sequences when adjacent numbers appeared.
    • What Made It Fun:
      • The sheer satisfaction of piecing together fragmented sequences into one long chain—it felt like connecting dots to reveal a hidden picture.

Insights From Today

  1. Hashmaps Are the MVP:

    • Whether it’s tracking indices or merging sequences, hashmaps repeatedly prove they’re indispensable. They simplify complex problems and make solutions elegant.
  2. Patterns Over Brute Force:

    • Both problems could’ve been solved with brute force, but stepping back and recognizing patterns (like tracking indices or sequence boundaries) made all the difference.
  3. Breaking Problems Into Stories:

    • Thinking of problems as stories—like “tracking a wandering duplicate” or “assembling a puzzle of consecutive numbers”—made them easier and more engaging to solve.

Reflections

Today reminded me why I enjoy competitive programming—it’s not just about solving problems; it’s about the journey, the “aha moments,” and the joy of discovery. Both challenges today pushed me to think creatively and appreciate the beauty of efficient data structures.


What’s Next?

With Week 4 wrapped up, I’m excited to plan Week 5, where I’ll tackle Graph Problems, Greedy Algorithms, and Dynamic Programming. The road ahead looks challenging but thrilling, and I’m ready to take it on.

Thanks for sticking with me on this journey! Let’s keep growing, learning, and solving together.

Top comments (0)