# Data Structures: Question Set – 35

#### What is collision in hashing, and how is it resolved?

Collision in hashing occurs when two or more inputs produce the same hash value. This is a common occurrence in hashing, as there are typically more possible inputs than there are possible hash values. Collision is resolved by using a collision resolution technique, such as chaining or open addressing.

#### What is chaining in hashing, and how does it work?

Chaining is a collision resolution technique in hashing where each bucket in the hash table contains a linked list of all the elements that hash to that bucket. When a collision occurs, the new element is added to the end of the linked list in the corresponding bucket. Chaining is a simple and effective method for handling collisions, but it requires additional memory to store the linked lists.

#### What is open addressing in hashing, and how does it work?

Open addressing is a collision resolution technique in hashing where the hash table is probed for an empty slot when a collision occurs. There are several ways to probe the table, including linear probing, quadratic probing, and double hashing. Open addressing requires less memory than chaining, but it can be more complex to implement and can lead to clustering, where elements are grouped together in the table.

#### What is a hash table, and how is it implemented?

A hash table is a data structure that uses hashing to store and retrieve data. It consists of an array of buckets and a hash function that maps keys to bucket indices. To store a key-value pair in a hash table, the key is first hashed to determine the bucket index, and the value is then stored in the corresponding bucket. To retrieve a value for a given key, the key is hashed again to find the bucket index, and the value is then retrieved from the corresponding bucket.

#### What are the advantages and disadvantages of using a hash table?

The advantages of using a hash table include:

- Fast retrieval: Hash tables can retrieve values for a given key in constant time on average.
- Flexible key types: Hash tables can support a wide range of key types, including strings, integers, and even complex objects.
- Space efficiency: Hash tables can use memory more efficiently than other data structures, especially when the number of keys is much smaller than the size of the hash table.

The disadvantages of using a hash table include:

- Collisions: Hash tables may suffer from collisions, where multiple keys hash to the same bucket index. This can reduce performance and requires collision resolution techniques.
- Unordered: Hash tables are not ordered, so iterating over the keys or values may not produce them in a specific order.
- Hash function quality: The performance of a hash table depends on the quality of the hash function, which can be difficult to design.

#### What is a perfect hash function, and how is it different from a general hash function?

A perfect hash function is a hash function that produces no collisions for a specific set of keys. This means that the hash function can be used to build a hash table without requiring collision resolution. A general hash function, on the other hand, is designed to work with any set of keys and may produce collisions, which must be resolved using collision resolution techniques.

#### What is a bloom filter, and how is it used in hashing?

A bloom filter is a probabilistic data structure that uses hashing to test whether an element is a member of a set. It consists of a bit array and multiple hash functions. To add an element to the filter, it is hashed using each of the hash functions, and the corresponding bits in the bit array are set to 1. To test whether an element is in the filter, it is hashed using the same hash functions, and the corresponding bits in the bit array are checked. If any of the bits are 0, the element is not in the filter. If all the bits are 1, the element may be in the filter, but there is a small probability of a false positive. Bloom filters are used in applications where space efficiency is more important than accuracy, such as network routers and spell checkers.

#### What is a hash-based message authentication code (HMAC), and how is it used in cryptography?

A hash-based message authentication code (HMAC) is a type of cryptographic hash function that uses a secret key to authenticate a message. It consists of a hash function and a secret key. To authenticate a message, the message is first combined with the secret key using a specific algorithm, and the resulting string is then hashed using the hash function. The resulting hash value is then used as the authentication code. HMACs are commonly used in network security protocols, such as TLS and SSL, to ensure the integrity and authenticity of messages.

#### What is collision resolution in hashing, and what are some techniques for resolving collisions?

Collision resolution is the process of handling situations where two or more keys hash to the same bucket index in a hash table. Some techniques for resolving collisions include:

- Chaining: In this technique, each bucket in the hash table is a linked list. When a collision occurs, the new key-value pair is added to the end of the linked list in the corresponding bucket.
- Open addressing: In this technique, when a collision occurs, the hash function is rehashed with a different value to find the next available bucket. This process is repeated until an empty bucket is found.
- Linear probing: In this variant of open addressing, the hash function is rehashed with a series of linear offsets until an empty bucket is found.
- Quadratic probing: In this variant of open addressing, the hash function is rehashed with a series of quadratic offsets until an empty bucket is found.

#### What is a hash collision, and how can it be avoided?

A hash collision occurs when two or more keys hash to the same bucket index in a hash table. Hash collisions can be avoided by choosing a good hash function that distributes keys evenly across the hash table. A good hash function should have the following properties:

- Deterministic: The hash function should always produce the same hash value for the same input key.
- Uniform: The hash function should distribute keys evenly across the hash table.
- Fast: The hash function should be computationally efficient.