Skip to content
DSA hashing 5 min read

Hashing Exercises: Practice Problems (with Solutions)

Time to practice. Solve each problem yourself first — write the code, trace a small example by hand, and reason about its time and space complexity. Only then open the answer sheet. Every View answer link jumps to a full, multi-language solution on the hashing solutions page.

How to practice: Set a timer (15–20 min per problem). If you’re stuck, re-read hashing applications & patterns before peeking. The goal is to recognize which hash pattern fits — counting, complement lookup, or grouping.

Warm-ups

Q1. Count frequencies. Given an array of integers, return a map (or dictionary) from each value to the number of times it appears. Target O(n) time.

View answer →

Q2. Contains duplicate. Return true if any value appears at least twice in the array, else false. Aim for a single O(n) pass using extra space.

View answer →

Core problems

Q3. Two Sum. Given an array nums and a target, return the indices of the two numbers that add up to the target. Beat the obvious O(n²) with a hash map.

View answer →

Q4. Group anagrams. Given a list of strings, group together the ones that are anagrams of each other. Return a list of groups.

View answer →

Q5. First non-repeating character. Given a string, return the index of the first character that appears exactly once, or -1 if there is none.

View answer →

Challenge

Q6. Subarray sum equals K. Given an integer array nums and an integer k, count how many contiguous subarrays sum to exactly k. Values may be negative. Target O(n) time using prefix sums and a hash map.

View answer →

Q7. Longest consecutive sequence. Given an unsorted array, return the length of the longest run of consecutive integers (e.g. [100,4,200,1,3,2]4 for 1,2,3,4). Target O(n) using a hash set — no sorting.

View answer →


Done? Review every solution on the hashing answer sheet — even the ones you solved, since there’s often a cleaner approach. Then continue to recursion.

Last updated June 25, 2026
Was this helpful?