Stage 4 · Lesson 5 of 10
Hash maps and sets
A stores key and value pairs. It runs each key through a , which turns the key into an array index. So lookup, insert and delete take O(1) on average.
Sometimes two keys land in the same slot, which is called a collision. The map keeps both, using or , and it grows its array as it fills so lookups stay fast. A set is the same idea with keys only.
This is the structure that most often turns a slow solution into a fast one. Checking "have I seen this?" in a list is O(n); in a set it is O(1). Keys must be (the language must be able to hash them), and should never change after they are inserted.
A question to check yourself, an AI practice session and 1 cheat sheet entry come when you add the roadmap to your account. Add it
Resources for this stage
Start with· follow one of these
- CoursePaidGrokking Data Structures & Algorithms for Coding Interviews
Design Gurus · Beginner · Subscription
Each structure built from first principles, with solutions in Python, Java, C++, JavaScript and C#
- CourseFreeCS50: Introduction to Computer Science
Harvard University · Beginner
Free: weeks 3 and 5 cover searching, sorting, linked lists, hash tables and trees
Practise
Keep handy
Also worth your time
Time and space complexity worked through five common algorithms