Hash Tables

Hash tables give O(1) average lookup, insert, and delete. Learn frequency counting and two-sum patterns.

40 lessons
114 min

Lessons

1. Intro

O(1) lookups

2m

2. Vocabulary - Hash Table

Key-value storage

3m

3. How Hashing Works

The basic idea

3m

4. Hash Table Operations

Average vs worst case

3m

5. Hash Set vs Hash Map

When to use which

3m

6. Pattern - Frequency Counting

Counting occurrences

3m

7. Frequency Counting Applications

Common uses

2m

8. Problem - Valid Anagram

Frequency comparison

3m1 problems

9. Valid Anagram - Algorithm

Two approaches

3m

10. Valid Anagram - Implementation

Complete solution

3m1 problems

11. Lessons from Valid Anagram

Frequency pattern

2m

12. Quiz: Frequency Counting

Time complexity

2m1 problems

13. The Pair Finding Problem

Beyond sorted arrays

2m

14. Pattern - Complement Lookup

Find the other half

3m

15. Problem - Two Sum

Classic hash map

3m1 problems

16. Two Sum - Algorithm

One pass solution

3m

17. Two Sum - Implementation

Complete solution

3m1 problems

18. Lessons from Two Sum

Complement pattern

2m

19. Quiz: Two Sum Pattern

Order of operations

2m1 problems

20. Problem - Group Anagrams

Hash with custom keys

3m1 problems

21. Group Anagrams - Key Design

Canonical form

3m

22. Group Anagrams - Implementation

Complete solution

3m1 problems

23. Lessons from Group Anagrams

Custom hash keys

2m

24. Combining Techniques

Prefix sum meets hash

3m

25. The Core Idea

Counting complements

4m

26. Problem - Subarray Sum Equals K

Prefix + hash combo

3m1 problems

27. Subarray Sum - Algorithm

Count as you go

4m

28. Subarray Sum - Why Initialize with 0

Handling edge case

3m

29. Subarray Sum - Implementation

Complete solution

4m1 problems

30. Lessons from Subarray Sum

Combined power

3m

31. Quiz: Prefix Sum + Hash

Initial value

2m1 problems

32. A Harder Problem

Sequence detection

3m1 problems

33. Longest Consecutive - The Idea

Start from sequence beginning

4m

34. Longest Consecutive - Algorithm

Two-phase approach

3m

35. Longest Consecutive - Implementation

Complete solution

4m1 problems

36. Lessons from Longest Consecutive

Amortized thinking

3m

37. Quiz: Hash Set Usage

Time analysis

2m1 problems

38. When Hashing Fails

Collision attacks

3m

39. Recognizing Hash Problems

Pattern signals

2m

40. Section Recap

What we learned

3m

Practice Problems

1.
Registration SystemCodeforceseasy

Classic introduction to hash maps for tracking username frequencies. Generate unique usernames by appending numbers.

2.
Lecture SleepCodeforceseasy

Combines prefix sums with sliding window. Teaches frequency-based optimization fundamental to hash table applications.

3.
Find PairCodeforcesmedium

Frequency counting combined with combinatorics. Use hash maps to find the k-th lexicographic pair efficiently.

4.
Glass CarvingCodeforcesmedium

Advanced use of multiset to track segment lengths dynamically. Teaches collision handling and efficient updates.

5.
Watto and MechanismCodeforcesmedium

Perfect introduction to polynomial string hashing. Check if string with one character difference exists in a set.

6.
Good SubstringsCodeforcesmedium

Classic problem for counting distinct substrings using polynomial hashing. Demonstrates why double hashing prevents collisions.

7.
PasswordCodeforcesmedium

Can be solved with KMP or string hashing. Find substring that appears as prefix, suffix, and middle.

8.
Compress WordsCodeforceshard

Excellent for mastering rolling hash for suffix-prefix matching. Find longest overlap between consecutive strings.

9.
Vasiliy's MultisetCodeforcesmedium

01-trie problem for maximum XOR queries. Teaches binary representation hashing and greedy bit traversal.

10.
Petya and ArrayCodeforcesmedium

Combines prefix sums with data structures. Hash-based discretization for counting subarrays with sum less than threshold.

11.

Masterclass in binary trie for XOR queries. Handle dynamic insertions/deletions while counting elements satisfying conditions.

12.

Advanced palindrome detection using bidirectional hashing. Compute hashes on both original and reversed strings.

13.
Two SumLeetCodeeasy

The quintessential hash map problem teaching the fundamental pattern of O(1) lookups while iterating.

14.
Valid AnagramLeetCodeeasy

Core frequency counting pattern using hash maps to compare character distributions.

15.
Group AnagramsLeetCodemedium

Advanced grouping pattern using sorted strings or character counts as hash keys for O(n) grouping.

16.

Brilliant O(n) solution using hash sets to identify sequence starts and extend them.

17.
Subarray Sum Equals KLeetCodemedium

The canonical prefix sum + hash map problem for counting subarrays in O(n) time.

18.
LRU CacheLeetCodemedium

Combines hash map with doubly linked list for O(1) operations on both access and eviction.

19.

Hash map + dynamic array enabling O(1) random access. Teaches swap-with-last-element trick.

20.
4Sum IILeetCodemedium

Meet-in-the-middle technique with hash maps reducing O(n^4) to O(n^2).

Ready to start learning?

Access all 40 lessons with interactive content and progress tracking.