Hash tables Flashcards

1
Q

What is a hash table?

A

A data structure that stores records containing keyed data items and uses a hash function to compute an array index from the key.

Hash tables aim for near constant-time (O(1)) average complexity for insertion and searching operations.

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

How does a hash function for integer keys work?

A

h(k) = k mod N, where N is the size of the hash table array.

This ensures the hash value is within the valid range of array indices (0 to N-1).

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

What is the suggested hash function for float keys?

A

hfloat(k) = hint(round(k)), where the float is rounded to the nearest integer before hashing.

The effectiveness of hash functions depends on the distribution of keys.

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

How can string keys be hashed?

A

hstring(k) = (Σ k[i]) mod N, where Σ k[i] is the sum of ASCII values of each character in the string.

Folding is an improved method where groups of characters are treated as integers.

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

What is the basic algorithm for inserting an item into a hash table?

A

Algorithm hashTableInsert(i): index = h(item.key()), table[index] = i

This assumes a perfect hash function without collisions.

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

What is a collision in a hash table?

A

When two different keys hash to the same array index.

Collisions are inevitable due to practical limitations on hash table size.

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

What are the four main methods for resolving collisions?

A
  • Open Addressing
  • Linear Probing
  • Quadratic Probing
  • Double Hashing
  • Separate Chaining
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is Open Addressing in collision resolution?

A

Probing the hash table for an alternative unused index when a collision occurs.

The sequence of alternative indices is determined by a probing function.

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

What is Linear Probing?

A

A collision resolution method where the next indices checked are (h(k) + j) mod N.

Linear probing can lead to clustering of occupied slots.

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

What is Quadratic Probing?

A

A probing method using a quadratic function for p(j), such as p(j) = (-1)^(j-1) * (ceil(j/2))^2.

This reduces clustering compared to linear probing.

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

What is Double Hashing?

A

A technique that uses a second hash function h2(k) to determine the probe increment.

The probing function is of the form p(j) = h2(k) * j.

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

What is Separate Chaining?

A

An approach where each index in the hash table points to a container ADT, typically a linked list, to hold items that hash to that index.

This method allows easy handling of collisions by adding new items to the linked list.

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

What is the load factor of a hash table?

A

LF = s/N, where s is the number of items stored and N is the size of the hash table array.

A high load factor increases the likelihood of collisions.

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

What happens when the load factor exceeds a certain threshold?

A

The hash table is resized, creating a larger array and rehashing all existing items.

This maintains overall efficiency despite being an expensive operation.

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

How is deletion handled in a hash table with open addressing?

A

Marking a slot as ‘available, but previously occupied’ to avoid breaking probe sequences.

This allows the search process to continue correctly.

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

What is the algorithm for searching an item in a hash table?

A

Algorithm hashTableSearch(k): index = h(k), check if item at index matches key, else continue probing.

If the index is empty, it indicates the item does not exist.

17
Q

True or False: Hash tables are considered a type of keyed dictionary.

A

True.

They support operations like looking up, searching for, and deleting items by key.

18
Q

What is the impact of a high load factor on performance?

A

Increases collision frequency, leading to longer search and insertion times, degrading performance towards O(n).

Keeping the load factor small is crucial for maintaining efficiency.

19
Q

Fill in the blank: The basic concept of a hash table is to use a _______ to map keys to array indices.

A

[hash function]

20
Q

Fill in the blank: The probing function in linear probing can be expressed as _______.

A

[p(j) = j]

21
Q

What is a special marker used for in deletion from hash tables?

A

To indicate a slot that was previously occupied but is now available.

This prevents breaking the probe sequence for other items.