Hash tables are a must-know data structure for technical interviews. They’re used to store unordered collections of
(key, value)pairs, in which the key must be unique. Item lookup by key, inserting a new item, or deleting an item are all fast operations – approximately
O(1). Because they give you quick and cheap insertion, deletion, and lookups, you’ll be able to use them to solve many different types of interview questions.
When should you use a hash table?
Some common interview questions in which you should probably use a hash table are:
- When there’s a unique, non-arbitrary identifier that you can use as a key for lookup. (Example: A caller ID function that uses a person’s phone number to retrieve their information.)
- You need to quickly determine whether an element belongs to a collection. In these cases, you can represent the collection as a hash table with the elements as keys. (Example: A Scrabble checker that determines if a word is valid or not.)
- You’re solving a problem about invariants. (Example: Checking whether a word has an anagram.)
To get a good basic introduction (or reintroduction) to hash tables and how you can use them, watch our Intro to Hash Tables tutorial video!
(For even more information about hash tables, check out the Hash Tables section in any of our Interview Practice learning plans.)
Practice makes perfect!
When you feel ready to practice on some real interview questions about this data structure, head to Interview Practice on CodeFights. Sign up for one of our technical interview learning plans. (No matter which one you choose, you’ll have access to the hash tables section.) You’ll learn more about them in our hash tables topic overview, and you’ll solve real interview questions about them. All that practice will get you good at recognizing when to use them and how to implement them during technical interviews!