Data structures hash tables and collisions study guide

This study guide covers key terms and concepts related to hash tables and collisions in data structures, crucial for understanding algorithms in computer science.

Noah2009·56 flashcards·56 questions
collegecomputer_sciencealgorithms
0
Known
1 / 56
0
Learning
Front

Hash Table

Tap to flip
Back

A data structure that stores key-value pairs, enabling fast data retrieval based on unique keys.

Tap to flip
Got it
Still learning

Quiz(56 questions)

Question 1 of 56

1. What is a hash table primarily used for?

Terms in this Study Set(56)

Hash Table Basics(16)

Hash Table

A data structure that stores key-value pairs, enabling fast data retrieval based on unique keys.

Key

A unique identifier that maps to a specific value in a hash table.

Value

The data associated with a key in a hash table, which can be of any data type.

Hash Function

A function that converts a key into an index for the hash table, ideally distributing keys uniformly.

Load Factor

The ratio of the number of entries to the number of buckets in a hash table, typically denoted as LF=fracnm\displaystyle LF = \\frac{n}{m}.

Bucket

A storage location in a hash table where one or more key-value pairs can reside.

Collision

Occurs when two keys hash to the same index in a hash table, leading to conflicts in storage.

Chaining

A collision resolution technique where each bucket contains a linked list of entries that hash to the same index.

Open Addressing

A collision resolution strategy where a new index is searched for an entry in case of a collision.

Resize Operation

An action that increases the size of the hash table and rehashes existing entries, typically when the load factor exceeds a threshold.

Hash Table vs. Array

Hash tables allow for constant-time average complexity for lookups, while arrays require linear time for searches.

Best Case Performance

In an ideal scenario with no collisions, the time complexity for lookups in a hash table is O(1).

Worst Case Performance

In extreme cases, such as all keys colliding, lookups degrade to O(n), where n is the number of entries.

Dynamic Resizing Cause

When the load factor exceeds a certain threshold, a resize operation is triggered to maintain efficiency.

Example of a Hash Function

A simple hash function could be: h(k)=kmod  m\displaystyle h(k) = k \mod m, where k\displaystyle k is the key and m\displaystyle m is the size of the table.

True or False: Hash tables allow duplicate keys.

False: Each key must be unique within a hash table.

Collisions and Resolution Techniques(20)

What is a collision in hash tables?

A collision occurs when two different keys hash to the same index in a hash table.

Explain separate chaining.

Separate chaining is a collision resolution technique where each index in the hash table points to a linked list of entries that hash to the same index.

True or False: Open addressing uses linked lists.

False. Open addressing resolves collisions within the hash table itself, not through linked lists.

What is linear probing?

Linear probing is an open addressing technique where the algorithm checks the next index sequentially until an empty slot is found.

Define quadratic probing.

Quadratic probing is an open addressing method where the interval between probes is the square of the attempt number, e.g., i2\displaystyle i^2.

How does double hashing work?

Double hashing uses a second hash function to calculate the step size for probing when a collision occurs.

Compare separate chaining and open addressing.

- Separate chaining uses linked lists. - Open addressing searches within the table. - Separate chaining can handle high load factors better.

What is a load factor?

The load factor is the ratio of the number of entries to the size of the hash table, typically denoted as nm\displaystyle \frac{n}{m}.

Explain rehashing.

Rehashing involves creating a new, larger hash table and re-inserting all existing entries to decrease collisions as the load factor increases.

What happens during a collision?

When a collision occurs, the hash table must apply a resolution technique to find an alternative place for the new entry.

What is chaining's main limitation?

Chaining can lead to increased overhead due to pointers in linked lists and might degrade performance if lists grow too long.

How does open addressing affect table size?

Open addressing can require larger tables, often needing to maintain a load factor below a certain threshold, typically 0.7.

Give an example of double hashing.

If the primary hash function is h1(key)\displaystyle h_1(key) and the secondary is h2(key)\displaystyle h_2(key), the probing sequence is h1(key)+iimesh2(key)\displaystyle h_1(key) + i imes h_2(key).

What is a primary clustering problem?

Primary clustering occurs in open addressing when a sequence of filled slots causes long runs of occupied slots, leading to performance degradation.

True or False: All collision resolution techniques are equally efficient.

False. Efficiency varies based on the load factor and the number of collisions.

Fill in the blank: In separate chaining, the hash table entries point to ___ .

linked lists.

What is the disadvantage of linear probing?

Linear probing can cause clustering, making it inefficient as occupied indices group together, slowing down the search.

Define the term 'resize' in hash tables.

Resizing a hash table involves changing its size to maintain efficient performance, often triggered by exceeding a load factor threshold.

How does quadratic probing avoid clustering?

Quadratic probing reduces the likelihood of clustering by using squared increments, spreading out the occupied slots more evenly.

What is the purpose of a hash function?

A hash function transforms input data into a fixed-size hash code, determining the index for storing values in a hash table.

Performance and Analysis(12)

What is time complexity of hash table search?

Average case: O(1) - Constant time due to direct indexing. Worst case: O(n) - Occurs during collision scenarios.

Load factor definition?

The load factor, denoted as nm\displaystyle \frac{n}{m}, represents the ratio of the number of elements (n) to the number of buckets (m) in a hash table.

True or false: Higher load factor improves performance.

False - A higher load factor increases the likelihood of collisions, which can degrade performance.

Compare open addressing and chaining.

Open addressing: Collision resolution by finding next available slot. Chaining: Each bucket holds a linked list of entries.

Define amortized analysis in hash tables.

Amortized analysis evaluates the average time per operation over a sequence of operations, rather than the worst-case for each operation.

What does resizing a hash table involve?

Resizing involves creating a new larger table and rehashing all existing keys to fit the new table size.

Impact of poor hash function?

A poor hash function can lead to clusters of collisions, resulting in degraded performance and increased time complexity.

What is the purpose of a hash function?

The hash function converts keys into hash codes, which determine the index for storing elements in the hash table.

What is a collision in hash tables?

A collision occurs when two different keys hash to the same index in the hash table.

Fill in the blank: Efficiency of hash tables is greatly affected by _____ .

The choice of hash function and load factor.

Describe worst-case performance scenario.

Worst-case performance occurs when all keys hash to the same index, leading to linear time complexity O(n) for operations.

What is the expected time for insertion in a well-designed hash table?

Expected time for insertion is O(1) when the load factor is maintained at a reasonable level.

Applications of Hash Tables(8)

Hash tables in web caching

Hash tables store frequently accessed web data for quick retrieval, reducing load times and server stress.

True or False: Hash tables can store unique keys only.

True. Each key in a hash table must be unique to ensure correct value indexing.

Use of hash tables in databases?

Hash tables enable fast data retrieval by using keys for indexed access, enhancing database performance. - Key-value storage - Quick lookups

Fill in the blank: Hash tables are used in ________ for managing user sessions.

Hash tables are used in web applications for managing user sessions.

How do hash tables support spell check?

Hash tables store dictionaries of words. When checking spelling, the application hashes the input word to quickly find it in the table.

Comparison of hash tables and arrays?

Hash tables provide faster lookups (O(1) average) compared to arrays (O(n)). Arrays require sequential access while hash tables allow direct index access.

Use of hash tables in social media?

Hash tables manage user profiles and connections efficiently, facilitating quick searches and updates. - Fast user lookup - Relationship mapping

Question: Why are hash tables used in caches?

Hash tables allow quick data retrieval, reducing access times for frequently needed data and improving application performance.

Questions in this Study Set(56)

1. What is a hash table primarily used for?

A.Storing key-value pairs for fast retrieval
B.Sorting large datasets
C.Implementing binary trees
D.Creating dynamic arrays

2. What is one primary benefit of using hash tables in web caching?

A.They allow for quick data retrieval.
B.They store data in a linear format.
C.They require more memory than traditional databases.
D.They only work with numeric keys.

3. What is the term for when two keys hash to the same index in a hash table?

A.Collision
B.Load factor
C.Probing
D.Chaining

4. What is the average time complexity of searching in a well-designed hash table?

A.O(1)
B.O(n)
C.O(log n)
D.O(n^2)

5. Which of the following best describes a key in a hash table?

A.A unique identifier for mapping to a value
B.A data type for storing values
C.An index used for sorting elements
D.A method for resolving collisions

6. Which of the following is NOT a typical use of hash tables?

A.Managing user profiles in social media
B.Sorting a list of numbers
C.Storing session data in web applications
D.Implementing spell check functionality

7. Which collision resolution technique uses linked lists at each index?

A.Separate chaining
B.Linear probing
C.Quadratic probing
D.Double hashing

8. If a hash table has 10 buckets and contains 50 elements, what is the load factor?

A.0.2
B.5
C.0.5
D.10

9. What is the value associated with a key in a hash table?

A.The data linked to the key
B.The hash index of the key
C.The load factor of the table
D.The size of the bucket

10. How do hash tables optimize database performance?

A.By allowing fast indexed access through keys.
B.By storing data in a sequential manner.
C.By requiring less memory than arrays.
D.By enabling complex queries.

11. True or False: Open addressing resolves collisions by storing entries in linked lists.

A.True
B.False
C.Depends on implementation
D.Only in separate chaining

12. True or false: A higher load factor always leads to better performance in hash tables.

A.True
B.False
C.Depends on the hash function
D.Depends on the key size

13. What does a hash function do?

A.Converts a key into an index for the hash table
B.Stores key-value pairs
C.Measures the load factor
D.Resolves collisions

14. In which scenario would you most likely use a hash table?

A.When you need to perform frequent lookups by user ID.
B.When you need to store a large list of items in order.
C.When you want to implement a search algorithm.
D.When you are designing a static data structure.

15. What is linear probing primarily concerned with when resolving collisions?

A.Searching sequentially for an empty slot
B.Using linked lists
C.Employing multiple hash functions
D.Reducing the load factor

16. Which collision resolution method requires more memory due to additional data structures?

A.Open addressing
B.Chaining
C.Linear probing
D.Quadratic probing

17. How is load factor defined in a hash table?

A.The number of entries divided by the number of buckets
B.The total size of all keys
C.The maximum size of the table
D.The ratio of collisions to entries

18. What is the average time complexity for lookups in a hash table?

A.O(1)
B.O(n)
C.O(log n)
D.O(n^2)

19. How does quadratic probing differ from linear probing?

A.It uses the square of the attempt number for probing
B.It requires linked lists
C.It uses double hashing
D.It cannot handle collisions

20. What does amortized analysis of hash table operations help illustrate?

A.Worst-case scenario for a single operation
B.Average time over a sequence of operations
C.Space complexity only
D.Efficiency of the hash function

21. What is a bucket in the context of a hash table?

A.A location for storing key-value pairs
B.A type of hash function
C.A method for resizing the table
D.A performance metric

22. Fill in the blank: Hash tables use ________ to efficiently map keys to values.

A.Linear searches
B.Hash functions
C.Binary trees
D.Arrays

23. What is the purpose of double hashing in collision resolution?

A.To use two hash functions to determine probing steps
B.To create linked lists
C.To increase the load factor
D.To resize the hash table

24. What is typically involved in resizing a hash table?

A.Increasing the number of hash functions
B.Creating a new larger table and rehashing keys
C.Reducing the number of elements
D.Clearing the existing table

25. What occurs during a collision in a hash table?

A.Two keys hash to the same index
B.A key is duplicated
C.The load factor exceeds a threshold
D.A bucket becomes empty

26. Why might a hash table be preferred over an array for certain applications?

A.Hash tables require less initialization time.
B.Hash tables provide O(1) average case access time.
C.Hash tables can store duplicate values.
D.Hash tables are easier to implement.

27. Which scenario illustrates a limitation of separate chaining?

A.Long lists can degrade performance
B.It uses too much memory
C.It cannot handle collisions
D.It has no overhead

28. What is the effect of using a poor hash function in a hash table?

A.Increased memory usage
B.Fewer collisions
C.Clusters of collisions
D.Faster access times

29. Which collision resolution technique uses linked lists?

A.Chaining
B.Open addressing
C.Linear probing
D.Double hashing

30. Which of the following describes the role of hash tables in spell check applications?

A.They store the list of valid words for quick verification.
B.They organize words in alphabetical order.
C.They create synonyms for every word.
D.They analyze sentence structure.

31. What does the load factor represent in a hash table?

A.Ratio of entries to table size
B.Number of collisions
C.Efficiency of the hash function
D.Maximum number of entries

32. What is the primary purpose of a hash function in a hash table?

A.To store data
B.To sort elements
C.To convert keys into hash codes
D.To ensure security

33. What is the purpose of open addressing in a hash table?

A.To find a new index after a collision
B.To store values in linked lists
C.To calculate the load factor
D.To hash keys into unique values

34. What is the process of rehashing in hash tables?

A.Creating a larger table and reinserting entries
B.Reducing the load factor
C.Using linked lists
D.Implementing double hashing

35. What scenario describes a collision in hash tables?

A.Two keys hashed to different indices
B.A key being deleted
C.Two keys hashed to the same index
D.Resizing the table

36. When does a resize operation typically occur in a hash table?

A.When the load factor exceeds a certain threshold
B.When there's no more space in a bucket
C.When all keys are unique
D.When the hash function is changed

37. What happens during a collision when a hash table is full?

A.The entry is discarded
B.The hash table must apply a resolution technique
C.No new entries can be added
D.It automatically resizes

38. The efficiency of hash tables is greatly affected by which factors?

A.Number of operations only
B.Choice of hash function and load factor
C.The size of the keys
D.Frequency of access

39. In comparison to arrays, hash tables provide what type of average time complexity for lookups?

A.Constant time average complexity
B.Linear time complexity
C.Quadratic time complexity
D.Exponential time complexity

40. Which of the following is a disadvantage of linear probing?

A.Can lead to primary clustering
B.Requires linked lists
C.Increases load factor
D.Is inefficient for small tables

41. What situation describes the worst-case performance scenario for hash tables?

A.All keys hash to unique indices
B.Operations are evenly distributed across buckets
C.All keys hash to the same index
D.The table is empty

42. What is the best-case performance scenario for lookups in a hash table?

A.O(1) time complexity with no collisions
B.O(n) time complexity with all keys colliding
C.O(log n) time complexity
D.O(m) time complexity where m is the number of buckets

43. What is meant by the term 'resize' in hash tables?

A.Changing the number of entries
B.Increasing the table size
C.Decreasing the load factor
D.Eliminating collisions

44. What is the expected time complexity for insertion in a well-designed hash table when the load factor is kept reasonable?

A.O(n)
B.O(log n)
C.O(1)
D.O(n^2)

45. What is the worst-case performance for lookups in a hash table?

A.O(n) time complexity with all keys colliding
B.O(1) time complexity
C.O(log n) time complexity
D.O(m) time complexity

46. How does quadratic probing help avoid clustering?

A.By using squared increments for probing
B.By linking entries
C.By increasing load factor
D.By not allowing collisions

47. What triggers a dynamic resizing operation in a hash table?

A.Exceeding a predetermined load factor threshold
B.Having a single entry in the table
C.Changing the data type of values
D.Removing all entries from the table

48. What is the main goal of a hash function?

A.To produce a unique index for each key
B.To determine load factor
C.To link entries
D.To manage collisions

49. Which of the following is an example of a simple hash function?

A.h(k) = k mod m
B.h(k) = k + 1
C.h(k) = k * 2
D.h(k) = k / m

50. Which of the following is NOT a collision resolution technique?

A.Separate chaining
B.Linear probing
C.Double hashing
D.Hash function adjustment

51. True or False: Hash tables allow duplicate keys.

A.False, each key must be unique
B.True, duplicate keys are allowed
C.False, keys can be repeated in buckets
D.True, but only in specific scenarios

52. Which statement about load factors is correct?

A.Higher load factors lead to more collisions
B.Load factor is always equal to 1
C.Load factor indicates the efficiency of a hash function
D.Lower load factors are inefficient

53. What defines primary clustering in open addressing?

A.Long runs of occupied slots
B.Using linked lists
C.The load factor
D.Rehashing frequency

54. In the context of double hashing, what does the probing sequence look like?

A.h1(key) + i * h2(key)
B.h1(key) + i^2
C.h2(key) - i
D.h2(key) + i

55. Which of the following best describes how linear probing resolves collisions?

A.It looks for the next available index sequentially.
B.It links entries at the same index in a list.
C.It uses a different hash function for each attempt.
D.It stores all entries in a separate table.

56. In separate chaining, what is the main drawback of using linked lists for collision resolution?

A.Increased memory overhead from pointers.
B.Faster search times compared to open addressing.
C.Reduced load factor efficiency.
D.No collisions can occur.

Related Study Sets

Create Your Own Study Set

Upload a PDF, paste your notes, or describe a topic – AI generates flashcards, quizzes and more in seconds.