Hash Functions and Hash Tables Flashcards

Memorizing hash functions, their reason of being, and how to implement hash tables

1
Q

What do they represent

A

Dynamic set of data

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

Which three functions do they support?

A

1) Insert
2) Search
3) Delete

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

What is the average runtime for their search operation?

A

O(1)

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

What is a dictionary in the world of code?

A

Generic way to map keys to values

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

What is a hash table

A

The implementation of a dictionary using a hash function

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

What is closed addressing/open hasing?

A

Where each slot in the table refers to a chain of entries, and each entry stores one (key, value) pair

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

What is open addressing/closed hasing?

A

Search for the next empty bucket within the hash table

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

What is the code to allocate a hash table and setting it’s elements to NULL?

A

dict_t *make_dictionary(void) {
dict_t *hashtable = malloc(sizeof(dict_t) * TABLESIZE);
assert(hashtable != NULL);
for (int i = 0; i < TABLESIZE; i++) {
hashtable[i] = NULL;
}
return hashtable;
}

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

What does the hash’s structure look like?

A

typedef struct entry {
char *key;
char *value; // value associated with the key
struct entry *next; // next entry in the linked list
} entry_t;

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

What is the run time to look up a value?

A

O(n)

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

What is the run time to look up a key?

A

O(1)

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

What is the code to print the contents of a dictionary?

A

void print_dictionary(dict_t *dict)
{
for (int bucket_index = 0; bucket_index < TABLESIZE; bucket_index++) {
printf(“%d: “, bucket_index);
entry_t *current_entry = dict[bucket_index];
if (current_entry == NULL) {
printf(“NULL\n”);
} else {
while (current_entry != NULL) {
printf(“%s: %s”, current_entry->key, current_entry->value);
current_entry = current_entry->next;
if (current_entry != NULL) {
printf(“, “);
}
}
printf(“\n”);
}
}
}

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

What is the code to be able to replace what is in a dictionary?

A

_Bool replace(dict_t *dict, char *key, char *value)
{
unsigned hash_index = hash(key);
entry_t *target_entry = search(dict[hash_index], key);
if (target_entry == NULL) {
return false;
} else {
free(target_entry->value);
target_entry->value = my_strdup(value);
assert(target_entry->value != NULL);
return true;
}
}

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

What is the code to clear a dictionary?

A

void clear(dict_t *dict)
{
for (int bucket_index = 0; bucket_index < TABLESIZE; bucket_index++) {
entry_t *current_entry = dict[bucket_index];
while (current_entry != NULL) {
entry_t *entry_to_free = current_entry;
current_entry = current_entry->next;
free(entry_to_free->key);
free(entry_to_free->value);
free(entry_to_free);
}
dict[bucket_index] = NULL;
}
}

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