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

A

constant

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
Q

what is the best ratio of items to indexes for best run time

A

items/indexes = 4

26
Q

when should the hash array be resized in separate chaining

A

doubled when n/m >= 8

halved when n/m <= 2

remember to rehash all keys when resizinf

27
Q

how do delete a key in separate chaining hash table

A

delete key from linked list

28
Q

what is linear probing also known as

A

open addressing

29
Q

how does linear probing work

A

when a key collides, find the next empty slot and put it in there

30
Q

how does search work in linear probing

A

search where the hash index is, if not there try the next and so on

31
Q

why is clustering common in linear probing

A

new keys are likely to be added in clusters as it will continuosly try the next space until a free one is found

32
Q

what is a requirement to use the linear probing algorithms

A

must always be one emmpty slot in the table

33
Q

which is better
many small clusters
few larger clustesr

A

many small clusters as searching will take less time if a blank space is found sooner

34
Q

on average how many indexes must you move to find the next empty space when the array is half full

A

M/2 spaces

35
Q

on average how many indexes must you move to find the next empty space when the array is full

A

M spaces

36
Q

if the number of indexes in the array in linear probing is too large why is this bad

A

takes up too much space

37
Q

resizing array parameters for linear probing

A

double when n/m > 1/2

halve when n.m < 1/4

38
Q

how to delete keys in linear probing hash table

A

delete and rehash everything after

39
Q

why must everything after be rehashed when deleting from a linear probing array

A

it will break the search function if there is an empty space

40
Q

what is the worst case scenario of linear probing

A

all the values are in one big cluster

41
Q

why is security often talked about with hash tables

A

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
Q

why is hashing used

A

to distribute entries uniformly across an array

43
Q

by using the key to search for an element, what is the runtime of such a sprt

A

constant run time

44
Q

what are the values returned from a hash function called

A

hash values
hash codes
hashes

45
Q

what three things are we looking for from a hash function

A

easy to compute (should not be a huge algorithm in itself)

uniform distribution across hash table

as little collisions as possible

46
Q

uses of hash tables

A

database indexing