Hashtables Flashcards

1
Q

a bad example of a way to hash phone numbers

A

using the first three digits

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

a good example of a way to hash phone numbers

A

last three deigits

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

what does the hashCode() method return

A

an int

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

what is an equality requirement of hash function

A

x.equals(y)

then x.hashCode() == y.hashCode()

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

what is highly desirable when it comes to equality and the hashCode() function

A

if !x.equals(y)
then
x.hashCode != y.hashCode()

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

what is the deafault implementation of hashCode

A

using memory address of the object

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

if a null object is sent into the hashCode function what should be returned

A

0

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

if an array is sent into the hashCode function how is it hashed

A

each array entry is hashed

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

the int returned must be between

A

-2^31 and 2^31 -1

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

how to convert if the key is not in the range

A

get abs value

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

what is the uniform hashing assumption

A

each key is equally likely to has to an integer between 0 and M-1

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

after how many insertions do we expect two keys in the same index

A

sq root (pi*M /2)

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

how many keys do we need to put in for all the indexes to be used

A

MlgM insertions

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

what do we expect the height of the most loaded index to ve

A

logM / loglogM

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

two methods for dealing with collisions

A

linear probing

separate chaining

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

what is a collision

A

when two keys hash at the same index

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

what is the separate chaining approach to dealing with collisions

A

have a linked list in every index of the array

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

when keys are inserted in the linked list implementation, where are they inserted, at the head or tail

A

head

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

what is the cost of inserting at the head of the linked list

20
Q

how to search for key in separate chaining approach

A

go to hash index

search through linked list

21
Q

worst case search using linked list approach

A

length of linked list

searches every node in the list

22
Q

what will the length of the longest linked list be

A

logM / loglogM

23
Q

if the number of indexes is too large in comparison to the number of items we want to hash, what is the problem

A

too many empty chains

24
Q

if the number of indexes is too small in comparison to the number of items we want to hash, what is the problem

A

chains are too long

25
what is the best ratio of items to indexes for best run time
items/indexes = 4
26
when should the hash array be resized in separate chaining
doubled when n/m >= 8 halved when n/m <= 2 remember to rehash all keys when resizinf
27
how do delete a key in separate chaining hash table
delete key from linked list
28
what is linear probing also known as
open addressing
29
how does linear probing work
when a key collides, find the next empty slot and put it in there
30
how does search work in linear probing
search where the hash index is, if not there try the next and so on
31
why is clustering common in linear probing
new keys are likely to be added in clusters as it will continuosly try the next space until a free one is found
32
what is a requirement to use the linear probing algorithms
must always be one emmpty slot in the table
33
which is better many small clusters few larger clustesr
many small clusters as searching will take less time if a blank space is found sooner
34
on average how many indexes must you move to find the next empty space when the array is half full
M/2 spaces
35
on average how many indexes must you move to find the next empty space when the array is full
M spaces
36
if the number of indexes in the array in linear probing is too large why is this bad
takes up too much space
37
resizing array parameters for linear probing
double when n/m > 1/2 | halve when n.m < 1/4
38
how to delete keys in linear probing hash table
delete and rehash everything after
39
why must everything after be rehashed when deleting from a linear probing array
it will break the search function if there is an empty space
40
what is the worst case scenario of linear probing
all the values are in one big cluster
41
why is security often talked about with hash tables
values can be chosen in a way that keys will be added to the same index of the array, or same cluster, grinding performance to a halt
42
why is hashing used
to distribute entries uniformly across an array
43
by using the key to search for an element, what is the runtime of such a sprt
constant run time
44
what are the values returned from a hash function called
hash values hash codes hashes
45
what three things are we looking for from a hash function
easy to compute (should not be a huge algorithm in itself) uniform distribution across hash table as little collisions as possible
46
uses of hash tables
database indexing