Hashing Data Structures Flashcards

1
Q

What are buckets?

A
  • A file is divided into a number of buckets (usually a prime number)
  • Records are stored in buckets
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the purpose of a hashing table?

A

Allows fast random access to <key,value> pair logical-records…

…Without any indexing overheads

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

What determines the bucket location a record is stored in?

A

A hashing function

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

How do hashing functions work?

A
  • The function will use the key field assigned to the record
  • Will return a number between 0 and the total number of buckets
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do collisions occur?

A

When two records with different key fields are assigned to the same bucket

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

What are 3 methods used to minimise collisions?

A
  1. Increase the number of buckets
  2. Have expandable buckets that can store a flexible amount of records
  3. Use overflow buckets
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the downsides of method 1:
Increase the number of buckets

A
  • A new hashing function is needed
  • All current data will need to be rehashed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the downsides of method 2:
Have expandable buckets that can store a flexible amount of records

A
  • Requires an in-bucket sequential search
  • No longer an O(1) search
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the downsides of method 3:
Use overflow buckets

A
  • Requires pointers from buckets to overflow area
  • Increases search time
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the probability of a collision occurring after adding 4 records to a file with 10 buckets?

A

Probability a collision has not occurred:
(10/10) x (9/10) x (8/10) x (7/10) = 0.504

Probability a collision has occurred:
1 - 0.504 = 0.496

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

What are 2 examples of hashing tables?

A
  1. HashSets <u></u>
  2. HashMaps <k, v>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a hash table?

A
  • A data structure that stores data via hashing
  • The index of each element is determined by a hash code
  • Hash code is calculated using a hashing function
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to search a hash table for an object?

A
  1. Compute the hash code and then reduce it to find the index/bucket.
    (Hash code MOD table size)
  2. Search the elements of the bucket
  3. If a match is not found, the object is not present
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the best case scenario when searching and storing data in a hash table?

A
  • Data will be distributed evenly among buckets, so that each bucket will only store 1 item
  • Time complexity O(1)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the worst case scenario when searching and storing data in a hash table?

A
  • All items have been stored in a single bucket
  • Time complexity O(n) as it’s a sequential search through every item of data
How well did you know this?
1
Not at all
2
3
4
5
Perfectly