DEV Community

Somuya Khandelwal
Somuya Khandelwal

Posted on

DAY 20 Closing the Week with Hashmap Challenges

Hello Readers!

Today marked the last day of Week 4 in my competitive programming journey, and I decided to switch things up with Hashmap Problems. Instead of breaking down everything step by step, I want to talk about how today felt more like solving puzzles—each piece had to fit perfectly. Hashmaps have quickly become my favorite tool; they’re efficient, versatile, and make solving problems feel both rewarding and fun when used effectively.


Today’s Challenges

  1. Contains Duplicate II (Medium Difficulty)

    • This problem was all about spotting duplicates within a given range k in an array. It felt like trying to track recurring faces in a crowd.
    • The Key Insight:
      • Using a hashmap to store the last-seen index of each number completely changed the game. As I iterated through the array, I simply checked if the current index and the stored index were within the required range.
    • Why It Was Fun:
      • It wasn’t just about finding duplicates—it was about doing it smartly, leveraging the hashmap to avoid unnecessary comparisons and keeping things efficient.
  2. Longest Consecutive Sequence (Medium Difficulty)

    • This one felt like assembling the longest chain in a scattered puzzle. The goal was to find the longest sequence of consecutive numbers in an unsorted array.
    • The Key Insight:
      • Instead of sorting the array, I used a hashmap to dynamically track and merge boundaries of sequences. By extending sequences whenever a new number fit, the problem turned into an elegant game of connecting pieces.
    • Why It Was Fun:
      • Watching fragmented sequences merge into a single, satisfying chain was incredibly rewarding—it felt like creating order out of chaos.

Key Takeaways

  1. Hashmaps Are Game-Changers:

    • Whether it’s tracking indices or merging boundaries, hashmaps make even complex problems manageable and elegant.
  2. Patterns Over Brute Force:

    • Both problems had brute-force solutions, but recognizing patterns—like tracking indices or managing sequence boundaries—turned them into efficient, creative challenges.
  3. Think of Problems as Stories:

    • Imagining problems as stories—like "tracking duplicates in a crowd" or "assembling pieces of a broken sequence"—helped me stay engaged and think more clearly about the solutions.

Reflections

Today reminded me why I love competitive programming—it’s not just about solving problems; it’s about the joy of discovery and those satisfying “aha moments.” Both challenges today pushed me to think outside the box and made me appreciate the beauty of elegant data structures like hashmaps.


What’s Next?

With Week 4 wrapped up, I’m ready to dive into Week 5, where I’ll be tackling Graph Problems, Greedy Algorithms, and Dynamic Programming. It’s going to be tough, but I’m excited to level up and face the new challenges ahead.

Thank you for joining me on this journey! Let’s keep growing, learning, and solving together.

Best regards,

Somuya

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay