Hash Tables / Hash Maps Flashcards

(50 cards)

1
Q

What is a hash table?

A

A data structure that maps keys to values using a hash function.

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

What is a hash map?

A

Another name for a hash table, often used interchangeably.

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

What is a hash function?

A

A function that converts a key into an index in the hash table.

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

What is the goal of a good hash function?

A

To distribute keys uniformly and minimize collisions.

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

What is a collision in a hash table?

A

When two keys hash to the same index.

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

What are common collision resolution techniques?

A

Chaining and open addressing.

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

What is chaining?

A

A method where each table index points to a linked list of entries that hash to that index.

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

What is open addressing?

A

A collision resolution method that finds another open slot in the table when a collision occurs.

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

What are the types of open addressing?

A

Linear probing, quadratic probing, and double hashing.

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

What is linear probing?

A

A technique where you check the next slot until an empty one is found.

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

What is quadratic probing?

A

A technique where you check slots at increasing quadratic intervals.

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

What is double hashing?

A

Using a second hash function to determine the step size when resolving collisions.

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

What is the average-case time complexity for search, insert, and delete in a hash table?

A

O(1)

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

What is the worst-case time complexity for hash table operations?

A

O(n), if many collisions occur.

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

What is the load factor of a hash table?

A

The ratio of the number of entries to the number of buckets.

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

Why is the load factor important?

A

High load factors increase the chance of collisions and degrade performance.

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

What is rehashing?

A

The process of resizing the hash table and re-inserting all elements.

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

When should you rehash a hash table?

A

When the load factor exceeds a certain threshold.

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

What is a key in a hash map?

A

An identifier used to retrieve its associated value.

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

What is a value in a hash map?

A

The data associated with a key.

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

Can hash maps store duplicate keys?

A

No, each key must be unique.

22
Q

Can hash maps store duplicate values?

A

Yes, values can be duplicated.

23
Q

What happens if you insert a key that already exists?

A

Its value is updated.

24
Q

What is a hash collision attack?

A

An attack that exploits poor hash functions by causing many collisions.

25
How do you prevent hash collision attacks?
Use strong cryptographic hash functions or randomized hashing.
26
What is a dictionary in Python?
A built-in implementation of a hash map.
27
What is a HashMap in Java?
A built-in hash table class from the Java Collections Framework.
28
What are some applications of hash tables?
Caching, indexing, symbol tables, and lookup operations.
29
What is the time complexity of rehashing?
O(n), since all elements must be re-inserted.
30
How is deletion handled in open addressing?
By marking slots as deleted (lazy deletion) or reordering.
31
What is a tombstone in hash tables?
A marker used to indicate a deleted entry during open addressing.
32
What is a hash set?
A hash table that stores only keys, not key-value pairs.
33
What is the difference between a hash table and an array?
Arrays use numeric indices; hash tables use keys and hash functions.
34
What is clustering in open addressing?
The buildup of many entries in a sequence of slots, reducing performance.
35
What is the primary benefit of using a hash table?
Constant-time average access for insert, search, and delete.
36
What is a perfect hash function?
A hash function that causes no collisions for a specific set of keys.
37
What is a universal hash function?
A family of hash functions designed to reduce the probability of collision.
38
What is an example use case for hash tables in compilers?
Symbol tables for storing variable names and scopes.
39
What is the difference between a hash table and a tree map?
Hash tables offer O(1) access; tree maps offer sorted keys with O(log n) access.
40
What is the default initial capacity of Java’s HashMap?
16
41
What is the default load factor for Java’s HashMap?
0.75
42
What happens when a hash table reaches its capacity?
It resizes (rehashes) to a larger table.
43
Can keys in a hash table be of any type?
Yes, as long as they are hashable (immutable and implement a proper hash function).
44
What makes a data type hashable?
It must implement a stable and consistent hash function and be immutable.
45
What is the modulo operator used for in hash functions?
To confine the hash value to a valid index range.
46
What is an example of poor hash function design?
Using the length of a string as the hash — it causes many collisions.
47
What is a common hash function for strings?
Polynomial rolling hash or djb2.
48
What is the purpose of equals() and hashCode() in Java?
They ensure consistent behavior for keys in a HashMap.
49
What is a multimap?
A hash table that allows multiple values for a single key.
50
How do you iterate through all keys in a hash map?
Use a key set or an iterator over the entry set.