Skip to content

hash tables are as delicious as a pretty nice steak

So I used to have this trick question I’d ask near the end of an interview, if I thought people could handle it (eg: they would know, at least, that a hash table was a programming concept). I would simply ask, “How would you go about sorting a hash table?”

The correct answer is to be confused by the question, and ask something along the lines of “Do you mean sort the keys, or sort the values, or something else entirely?”

Based on some recent things happening, I think a better way would be:

“How would you access the fourth item of a hash table?”

It’s the same concept – that hashes are not ordered in any way – but it’s something that yields a better answer both if you do or do not know the answer. Simply put, “You can’t, that makes no sense, I don’t want to work here if you think that’s possible” if you know what a hash table is, or something stupid if you’ve used them and don’t understand them:

@keys = keys(%hash)
print $hash{$keys[3]} // zero indexed; I’m a computer programmer

Even better is you could say something like “are you sure that is right?” and then get this:

@keys = keys(%hash);
if (@keys > 3)
{
print $hash{$keys[3]}; // zero indexed; I’m a computer programmer
}
else
{
print “Define some more variables!”;
}

Which is even better.

Published inGeneral

3 Comments

  1. Brian Brian

    You’ve really narrowed your audience since moving to Google.

  2. Brian Brian

    Alright, I looked up “hash table” on wikipedia just so I could understand what your post is about. All it did was reconfirm my ineptitude for computer programming. I could stare at that explanation for hours and I still won’t understand why the existance of a hash table is neccessary.

  3. A hash is basically just an array, except it’s indexed by anything (instead of just a number). It provides quick lookup time too.

    So, for example, if you have a list of people and their addresses, you could have a hash where

    “bob” -> “123 west st”
    “sam” -> “234 west st”
    “julie” -> “123 west st” # shacking up with bob. sinner.

    So the advantage is if you want bob’s address, you just say $hash{“bob”}, and there you have it. (Lookup time here is constant, no matter the size of the hash, as opposed to other data structures where you might spend more time finding the entry for “bob”).

    The thing is, since it’s not stored in numerical order (like an array, where you have a first element, second element, etc…) there’s no real concept of “what is the 2nd item?”

Leave a Reply