Hash tables Flashcards

(22 cards)

1
Q

What is a hash table used for?

A

Efficiently storing and retrieving key-value pairs.

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

What is the basic idea of hashing?

A

Map a key to an array index using a hash function.

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

Why can’t we use direct addressing for all keys?

A

Because the key space may be sparse and memory usage would be inefficient.

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

What is a collision in a hash table?

A

When two different keys map to the same index.

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

How does chaining handle collisions?

A

Each slot holds a linked list of entries with the same hash index.

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

What is open addressing?

A

A method of resolving collisions by probing for the next free slot in the array.

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

What is linear probing?

A

A probing strategy where we search sequentially for the next free slot.

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

Why is deletion hard in open addressing?

A

Removing a key breaks the probe chain and can make some entries unreachable.

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

What is a tombstone in open addressing?

A

A special marker used in place of a deleted entry to preserve the probe chain.

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

Why are tombstones necessary?

A

To ensure that searches can continue past deleted slots and find displaced entries.

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

What problem do tombstones create over time?

A

They accumulate and degrade performance unless periodically cleaned up.

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

What is the load factor (α) in a hash table?

A

The ratio of the number of items (n) to the table size (M): α = n/M.

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

What is the average complexity of put/get in open addressing?

A

Approximately Θ(1 + 1/(1 - α)) under uniform hashing.

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

What is primary clustering?

A

The formation of large contiguous blocks of occupied slots in linear probing.

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

How can primary clustering be reduced?

A

By using quadratic probing or double hashing instead of linear probing.

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

What makes a good hash function?

A

One that distributes keys uniformly and depends on all characters and their positions.

17
Q

What is an example of a good hash function for strings in Java?

A

hash = hash * 31 + char; 31 is chosen for performance and distribution.

18
Q

What happens if your table size M interacts poorly with your hash function?

A

The hash function’s effectiveness is reduced — may ignore most input characters.

19
Q

What is the complexity of rehashing?

A

O(n) — but amortized over many operations, the average cost remains low.

20
Q

How does chaining compare to open addressing in terms of worst-case performance?

A

Both degrade to O(n) in the worst case, but chaining handles high load factors better.

21
Q

Why is resizing a hash table necessary?

A

To maintain low load factor and efficient performance.

22
Q

What is the impact of a high load factor on open addressing?

A

It increases probe lengths and degrades performance.