Hash Tables Flashcards

(8 cards)

1
Q

What is a hash table?

A

A data structure, typically an array, which contains a set of unique keys, and values associated with these.

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

What is the main advantage of a hash table?

A

Data can be retrieved with a constant time complexity of O(1).

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

What are the steps of the hashing process?

A
  1. Apply a hash algorithm to the key value.
  2. The result returned will be the index of where to store the data.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a collision?

A

When the hash function produces the same index for two or more keys.

This is a problem. If multiple items are attempted to be stored I. The same memory location, data will be overwritten.

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

What are the solutions to dealing with collisions?

A

Solution 1: Store multiple values at the same index using a linked list.

Solution 2: Use the next available free memory location.

Solution 3: Rehash using a larger table with a different hash function.

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

What are the steps involved in rehashing?

A
  1. A larger hash table is created
  2. Each value in the existing table is inserted into the new table
  3. In a position determined by a new hashing algorithm
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the advantages of hash tables?

A

Faster to look up/insert a record (O(1)) compared to arrays/linked lists.

Used on large datasets where frequent access/modifications are needed.

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

What are the disadvantages of hash tables?

A

Collisions require additional handling.
Data stored is unordered.
Larger amount of memory used due to need for available memory locations.

Do not use if data needs to be sorted e.g. processed in a specific order.

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