News for hackers. Stuff that matters.
Re:Google Interview University – Plan for studying to become... by rand_r on Thursday October 06, 2016 @09:55AM (#12652435)
Totally pedantic, but no HashMap.get function is O(1). In order do a look-up, you have to compute the hash, whose size is at least log2(n).
Re:Google Interview University – Plan for studying to become... by dahart on Thursday October 06, 2016 @11:35AM (#12653324)
The reason hash computation is traditionally called constant time is because it doesn't change while the hash table grows. Big O describes how run time or memory consumption grows as input grows, and with a hash table, the hash computation typically does not grow.

Calling a hash table O(log2(N)) gets misleading when you measure a hash function's performance, and it doesn't change as the table grows.

So you're right about your technicality, but you're using big-O slightly differently than how everyone else is using it.

The implications are interesting, in that if the hash computation did grow as O(log2(N)), then it would (theoretically) outperform the current typical O(1) hash table implementation, and they would meet only when the table is full.

Another way to see O(1) is that hash computation is always implemented not as a log2(N) function, but is log2(MAX_N), where MAX_N is the size of a full table, and since MAX_N is a constant, the complexity is O(1).

A side note is that not all hash tables are implemented using an array that can hold N items. Some hash tables use linked lists instead of re-hashing for collisions, meaning that your array can be arbitrarily less than N in size, meaning that it is definitely possible to have a hash function that takes less than log2(MAX_N) time. In that case, insertion time becomes explicitly greater than O(1).

Re:Google Interview University – Plan for studying to become... by bogomipz on Thursday October 06, 2016 @12:59PM (#12654031)
I'm not following your logic here:

"The implications are interesting, in that if the hash computation did grow as O(log2(N)), then it would (theoretically) outperform the current typical O(1) hash table implementation, and they would meet only when the table is full."

How would O(log n) ever out perform constant time, O(1)? Could you elaborate?

Re:Google Interview University – Plan for studying to become... by dahart on Thursday October 06, 2016 @01:31PM (#12654336)
This is and always has been an important part of understanding what big-O notation actually means. O(1) means constant time, it does not tell you what the constant is. The constant can be large.

In this case, if your hash always takes 64 bit values and always produces 64 bit values, it is O(1). If your hash were O(log2(N)), then your hash function with an empty table would do 1 bit worth of work, and would do only one operation. But when the table grew to contain 1,000 elements, it would do 10 bits worth of work.

The O(log2(N)) algorithm with N < 2^64 is always faster (in theory) than the O(1) algorithm that does a constant 64 bits of work.

The O(1) algorithm pays off in spades for N >> 2^64, it will become much faster than the logarithmic one, but for small N, log(N) is smaller than 64.

Re:Google Interview University – Plan for studying to become... by bogomipz on Thursday October 06, 2016 @01:51PM (#12654503)
Right, a constant i.e unchanging amount of work.

I usually think of hashtables though as being O(1) in referring to the look up(amortized) and not the hash code itself.

Re:Google Interview University – Plan for studying to become... by dahart on Thursday October 06, 2016 @02:14PM (#12654693)
Yep, and a hash table's get is O(1) amortized even when you include the hash function itself, simply because the hash function doesn't change as the hash table grows. @rand_r is right that the hash function is doing C * log2(MAX_N) operations, but that doesn't actually affect the big-O metric, it only affects the run time. :P
Re:Google Interview University – Plan for studying to become... by bogomipz on Thursday October 06, 2016 @04:01PM (#12655611)
Indeed, thanks for the clarification.
Re:Google Interview University – Plan for studying to become... by conjectures on Thursday October 06, 2016 @10:08AM (#12652532)
Not being funny, I actually don't know:

Is the n in log2(n) the size of thing you're hashing? AFAIK the O(1) statement is referring to how many things you have hashed.

Re:Google Interview University – Plan for studying to become... by rand_r on Thursday October 06, 2016 @10:10AM (#12652555)
In context, n is the number of things in the hashmap. You need log2(n) bits per item to store n unique items. So the hash function is operating on an input of log2(n) bits. There's no way the hash function takes less than log2(n) time to work on log2(n) bits of input.
Re:Google Interview University – Plan for studying to become... by lkitching on Thursday October 06, 2016 @10:44AM (#12652845)
You don't need hash codes to be unique for items in the table so there doesn't need to be any relation between the size of the hash and the size of the table. The time taken by the hash function depends on the size of the keys not the size of the table.
Re:Google Interview University – Plan for studying to become... by rand_r on Thursday October 06, 2016 @10:49AM (#12652907)
If they're not unique, it will have to linearly search through items with duplicate keys. This situation will begin to approach O(n) time, which isn't good.
Re:Google Interview University – Plan for studying to become... by krona on Thursday October 06, 2016 @11:07AM (#12653101)
Unless you're using an Open addressing scheme like cookoo which ensures constant lookup time in the worst case
Re:Google Interview University – Plan for studying to become... by to3m on Thursday October 06, 2016 @10:58AM (#12653010)
If the cost of a hash function is related to anything, it is more usually the number of bits in the input.
Re:Google Interview University – Plan for studying to become... by FT_intern on Thursday October 06, 2016 @10:40AM (#12652809)
I've always used the assumption that objects have pre-calculated/stored hashes