The Importance of Hash Tables

On Coderbyte there are dozens of challenges that can be solved easily by implementing a hash table to store elements which you can then lookup quickly later on in your program.

Beginner coders solving the easy and medium challenges on Coderbyte frequently underestimate the value and broad applications of hash tables. For clarification, a hash table is a data structure that is created when you hash a value and then store all those hashed values. Essentially, if you have a list of names, you can create a hash table out of all those names.

At first glance, this seems superfluous. You just converted a readable list (or array) of names into useless characters! But hash tables have a magical property that speeds up certain critical and oft-used operations. Especially the type of operations that get asked at interviews!

For example, imagine you had a list of 1,000,000 items that people recently bought on a website like Amazon, and you wanted to check if the book, “The Great Gatsby”, was within that large list. You could simply loop through all the items looking for “The Great Gatsby” but that will, in the worst case scenario, require your program to perform 1,000,000 checks to see if each and every value is equal to “The Great Gatsby.”

Instead, if you were to hash all the items and store them in a hash table, checking for virtually any element would happen quickly and at the same speed no matter how large your dataset. Even though it takes a few more lines of code, it executes a lot faster. Below is an example using JavaScript.

Typical questions that rely on hash tables contain phrases like:

  • Search for elements within a large data set
  • Find duplicate elements in a data set
  • Quickly store and retrieve elements from a large data set

On Coderbyte, we provide several beginner and intermediate challenges that can be solved efficiently with hash tables:

The majority of beginner users on Coderbyte tend to answer these questions by simply writing a nested loop to search for some character or string within a data set. The efficiency provided by hash tables may not matter as much when you’re drawing on a whiteboard or solving a simple coding challenge, but it sure does matter when it comes to real applications with millions of data points (names, emails, database rows, etc.). In those cases, the difference between writing a series of nested loops and using hash tables can be the difference between your program running for seconds and running for days.

Don’t hesitate to comment with any questions or reach out directly to hello@coderbyte.com, and someone from our team will be in touch shortly. If you found this article helpful please click ♥ below.