Hash Tables/Dictionaries Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

When are Hash tables most commonly used?

A

When speed of look-up/deletion/insertion of items is a priority

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

What is a hash table?

A

An array which has been coupled with a hash function

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

What can hash tables help implement?

A

Hash tables can be used to implement dictionary data structures

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

What is a hash function?

A

A piece of code that takes in data (key) and returns a value (the hash value)

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

What is the hash value used for?

A

It maps the initial key to a fixed index in the hash table

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

What happens when you first use the hash function?

A

It tells us where to store a piece of data in the hash table for a given key

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

Why can we use the hash value to determine where in a hash table an item is stored?

A

Because the hashing function will always return the same value for a given key

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

What happens if the hash function generates the same value for different keys?

A

As there is already a value stored in that location, a collision will occur

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

What is a collision in hash tables?

A

When a hash function generates the same value for multiple keys

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

What is a solution for dealing with collisions in hash tables?

A

One solution is to store the second generated values key in the next available location/index

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

What should a good hash function be designed to do?

A

It should be designed to try to avoid creating duplicate hash values (however this may not be possible)

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

What is continuing to store keys in the next available location known as?

A

It is known as clustering

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

What is the problem with clustering?

A

It increases the probability of future collisions

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

What is the best solution for dealing with collisions in hash tables?

A

The best solution is to use linked lists with the hash table.

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

What happens when a collision occurs when using hash tables with linked lists?

A

If a collision takes place, the item is stored in overflow linked lists

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

Why is using linked lists more efficient than using the clustering method?

A
  • Linked lists allow for random fast access due to the array type data structure)
  • It also avoids increasing the probability of future collisions
17
Q

What is a dictionary?

A

An abstract data type consisting of associated pairs of values. It’s used for storing groups of objects

18
Q

What does a dictionary data structure consist of?

A

It consists of a set of keys, each key has a single value associated with it

19
Q

How is data retrieved in a dictionary data structure?

A

When the key is supplied the associated value is returned

20
Q

What should an implemented dictionary be able to do?

A
  • create a new empty dictionary
  • add a key:value pair
  • Delete a key:value pair
  • amend the value in a key:value pair
  • Return the value associated with a key
  • Return True/False to a statement depending if a key exists in the dictionary
  • Return the length of the dictionary
21
Q

What order are pairs held in a dictionary?

A

They are not held in any specific order

22
Q

Why is look up and retrieval quick and easy in dictionaries?

A

Because the key is hashed and placed in the resultant location

23
Q

Give an example of a common use of dictionary data structures.

A

They can be used in dictionary encoding (a type of compression)

24
Q

What is the code for creating a dictionary?

A

age = {‘Craig’ : 39, ‘Dave’ : 21, ‘Ella’ : 45}

25
Q

What is the code for returning a value in a dictionary?

A

> > > age [‘Craig’]

39

26
Q

What is rehashing?

A

The process of finding an empty slot when a collision occurs

27
Q

Give a real life example of what a hash table can be used for?

A

For storing user codes and encrypted passwords that need to be verified quickly