Hash Tables Flashcards

1
Q

Hash tables

A
  • store key value pairs

- usually are a fixed size array of some prime number size (that is larger than the num of elements)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Hash function

A

takes in a thing and returns an unsigned integer value which is then mod’ed by the size of the table and places there!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Three required properties of fast hash functions

A
  • must be deterministic (same value every time for a key)
  • must be fast
  • must be evenly distributed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Integer overflow is fine , as long as the function is deterministic meaning??

A

Overflow will almost always occur, just make sure the function returns the same num every time!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Two ways to resolve collisions?

A

Separate chaining

Open Addressing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Three types of open addressing?

A

Linear probing
Quadratic Probing
Double Hashing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Separate Chaining

A

All keys map to a linked list, which can add additional values that are mapped

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Load factor

A

ratio of number of elements divided by table size

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

In separate chaining, the load factor is….

A

the average number of elements in a bucket

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Average time of a unsuccessful find?

A

Load factor!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Average time on a successful find?

A

1 + (loadfactor/2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Smaller the load factor, the more memory it uses, but the few collisons!

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Bigger load factor uses…

A

less memory but is slower

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Why is it necessary to keep each key in the chain as well as the value?

A

Because otherwise you can’t be sure which collision value is the value you’re looking for!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Linear probing checks spots in what order starting with hash(k)

A
hash(k)+1
hash(k)+2
hash(k)+3
hash(k)+4
......
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Probs with linear probing?

A

large blocks of occupied cells you have to check

-holes when an element is removed…

17
Q

quadratic probing order

A

hash(k)
hash(k)+4
+9
+16

18
Q

Double hashing

A

2 hash functions!

hash(k)
hash(k) +1hash2(k)
hash(k)+2
hash2(k)

19
Q

Wjhy does the table size have to be prime with double hashing?

A

So that it will prevent thrashing, and also will lead to a better distribution

20
Q

Rehashing

A
  • when table is too full (load factor is too high), runninng time increases, so you should rehash
  • when insert fails DEFINITELY rehash
21
Q

Rehashing means

A

creating bigger array and rehashing each value already in the array

22
Q

How to remove an element in open addressing?

A
  • rehash upon every delete (not a good idea)
  • put in a placeholder value that you know is fake!… however the table would get filled with placeholders on the faster side…
23
Q

Pigeonhole principle

A

There would be at least one hash value with multiple keys… making an object unable to be backtraced to its key