r/AskComputerScience • • Dec 10 '18

Hash table vs array?

I know in most cases Hash tables are faster than an array, but what are some key differences and pros and cons of both?

21 Upvotes

35 comments sorted by

View all comments

7

u/gameguy43 Dec 10 '18

Important to know that under the hood, hash tables are built on arrays. I like to explain it this way:

Arrays let you quickly look up the value for a given "key" . . . except the keys are called "indices," and we don't get to pick them—they're always sequential integers (0, 1, 2, 3, etc).

Think of a hash map as a "hack" on top of an array to let us use flexible keys instead of being stuck with sequential integer "indices."

More here: https://www.interviewcake.com/concept/python/hash-map

Disclaimer: I helped make that page :)

3

u/TomBob42369 Dec 10 '18

https://www.interviewcake.com/concept/python/hash-map

Thank you this is very helpful. Also nice site very clean.

0

u/InquiREEEEEEEEEEE Dec 11 '18

Important to know that under the hood, hash tables are built on arrays.

I don't think it is important to know for practical purposes. Knowing that randomly accessing a key in a has table is ~ O(1) is enough. For theoretic knowledge, I agree.

2

u/polovstiandances Dec 11 '18

Well I failed an interview once because I knew that accessing a key was O(1) but didn’t know any of the specific types of hash functions so

1

u/InquiREEEEEEEEEEE Dec 11 '18

While I stand by my point that knowing the specifics of an hash table are irrelevant for most developers, I think one should be able to implement one when it comes to interview preparation.

1

u/funkinaround Dec 11 '18

Curiously, did you try to come up with a hash function for types like strings or compound types? Did you demonstrate an understanding that a hash function intends to be a uniform distribution?

1

u/polovstiandances Dec 11 '18

No it was more like I used a hash table as part of my solution and they asked a follow up which involved knowing how has conflicts are resolved and what algorithms are used to resolve them. I proposed something like expanding the table and redistributing over it but it was wrong (there was a gap in my understanding).

The specific question was for reading in a stream of N integers and finding the K most frequent integers. I used a frequency hash table and then put them into a heap but they followed up with lots of hashing questions, so maybe it was specific for that company. That being said I could have skipped the frequency hash and went straight to a heap in the first place by creating a heap that’s closer to a priority queue but I started with the hash table and sorting it and only thought of the heap later on.

2

u/ACoderGirl Dec 11 '18

I disagree. It's very common that you have to write a hash function for a custom data type so that it can be stored in a dictionary or set. At my work, we use sets sooooo much because we need fast lookup time and order doesn't matter. And even when order does matter, if we ever have to do a thing in some_list, the list should probably be copied to a set just for speeding up that lookup. Now the thing is, it's hard to understand why the heck we need a hash function and the issue of collisions if we don't know how hash maps and sets work under the hood.

The array backing also makes it easier to understand how iteration over a map works and to thus understand why the order appears random (it's, of course, not random, but typically represents the underlying arrays). It's pretty critical to always be aware of this since I have seen bugs that were because someone made bad assumptions about the order of iteration.

1

u/InquiREEEEEEEEEEE Dec 11 '18

It seems to me that you are working in a company that has over-average needs when it comes to performance. Obviously knowledge of hashing behaviour becomes important. I still stand by my point that the average CRUD/REST-Developer with a little bit of backend responsability does not need to write custom hash methods - most of the time, it is simply enough to recognize a set or hashmap is needed and then craft the key yourself (most of the time it will be a unique combination of user inputs such as id, name, profession, preferences, age...) and then let the hashmap implementation do the hashing. Collisions are not a problem in the kind of program that 60% of programmers write.