Hash Table Flashcards

1
Q

What is the name of the function a Hash Table uses to create its indexes?

A

A Hash Function.

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

Define a Hash Function.

A

A Hash Function will take all of the keys for a given map and strategically map them to certain index locations in an array so that they can eventually be retrieved easily.

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

What is the BigO of finding the index of a key?

A

O(1). Near instantaneous.

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

What is the backing Data Structure of a HashTable?

A

An array. The keys map to indexes in an array.

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

What happens when our hash function comes up with the same index value for two separate keys? For example both the key “Steven” and the key “Steve” both compute to the index 9.

A

A “Hash Collision” occurs.

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

Why do we use a hash function to come up with a numerical index?

A

To allow the use of an Array to store our data.
To minimise the size of the array. Our hash function squashes our keys down to a minimised index range so that we can have arrays sized relatively small compared to the range of our key values. E.g. key is 9 Billion, we could hash it to within a range of 1,000.

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

What are the two possible ways to handle a “Hash Collision”?

A
  1. Open Addressing

2. Closed Addressing

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

What is Open Addressing?

A

Put the key in some other index location separate from the one returned to us by the hash function. Usually the next OPEN (or free/available/nil) index.

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

What are the downsides of Open Addressing?

A
  1. Makes it harder to interact with the data in the map later on.
  2. Leads to issues when another key hashes to the (previously nil) used up index.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is Closed Addressing?

A

Uses Linked Lists to chain together keys which hash to the same index value.

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

What are the downsides of Closed Addressing?

A

To get to the key we want we have to traverse the LinkedList stored in the ‘Collision Index’. This slows down the access time from O(1) to O(n). n being the size of LinkedList, i.e. the number of collisions for that particular index.

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

Why is it difficult to do BigO calculations on HashTables?

A

Because we always assume the worst-case scenario. For a Hash Table this means every single key hashes to the same index value, which means (using Closed Addressing) we essentially end up with a single Linked List which contains all our elements. In this scenario all BigO calculations are that of the Linked List… O(n).

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

Why do we want a reliable Hash Function?

A

To minimise Hash Collisions.

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

Knowing that the worst-case scenario for Hash Tables is terrible, what are the BigO results for the average-case scenario for a Hash Table?

A

They are all O(1)!
To ACCESS, SEARCH for, INSERT, or DELETE a key/value pair from out dictionary, all we need to do. is run that key though the has function and it’ll tell us what index in the hash table to go to in order to perform that operation.

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