Podcast
Questions and Answers
What is a characteristic of a perfect hash function?
What is a characteristic of a perfect hash function?
- It can work efficiently with an unknown number of keys.
- It cannot map search keys to locations in a hash table.
- It allows for collisions to occur without issues.
- It maps each key to a unique slot when all keys are known beforehand. (correct)
Which of the following best describes double hashing?
Which of the following best describes double hashing?
- It can only be used with separate chaining.
- It is a method that increases the chances of clustering.
- It is the same as linear probing.
- It is a technique that significantly reduces clustering. (correct)
What happens when a search key that already exists in a dictionary is added again?
What happens when a search key that already exists in a dictionary is added again?
- The dictionary grows in size.
- The value associated with the key is updated. (correct)
- The old value associated with the key is discarded.
- The operation fails with an error.
Which statement regarding linear probing is accurate?
Which statement regarding linear probing is accurate?
Which statement about collisions in hashing is true?
Which statement about collisions in hashing is true?
What happens if an attempt is made to add a duplicate value to a set?
What happens if an attempt is made to add a duplicate value to a set?
Which method of collision handling uses a linked list at each hash index?
Which method of collision handling uses a linked list at each hash index?
What is the main issue associated with linear probing in hash tables?
What is the main issue associated with linear probing in hash tables?
What is a primary requirement of a hash function to ensure effective distribution of keys?
What is a primary requirement of a hash function to ensure effective distribution of keys?
What is the average time complexity for searching an element in a hash table?
What is the average time complexity for searching an element in a hash table?
What is the load factor of a hash table if there are 650 entries and the table size is 1500?
What is the load factor of a hash table if there are 650 entries and the table size is 1500?
Which programming statement correctly compresses a hash code to a valid array index?
Which programming statement correctly compresses a hash code to a valid array index?
Which aspect of hash tables indicates that the output must only depend on the input?
Which aspect of hash tables indicates that the output must only depend on the input?
Flashcards
Hash Table
Hash Table
A data structure that stores key-value pairs using a hash function to map keys to locations in an array.
Hash Function
Hash Function
A function that converts a key into a numerical index (hash code) for storage in a hash table.
Hash Code
Hash Code
A numerical value produced by a hash function that determines the position of an element in the hash table.
Collision
Collision
Signup and view all the flashcards
Chaining
Chaining
Signup and view all the flashcards
Load Factor
Load Factor
Signup and view all the flashcards
Set
Set
Signup and view all the flashcards
Time Complexity (Hash Table)
Time Complexity (Hash Table)
Signup and view all the flashcards
Perfect Hash Function
Perfect Hash Function
Signup and view all the flashcards
Re-hashing
Re-hashing
Signup and view all the flashcards
Collision in Hashing
Collision in Hashing
Signup and view all the flashcards
Linear Probing
Linear Probing
Signup and view all the flashcards
Key-Value Pair
Key-Value Pair
Signup and view all the flashcards
Study Notes
Sets
- Sets are collections of unique elements, designed for efficiency.
- Duplicate elements are not allowed.
- Removing an element that isn't present doesn't trigger an exception.
- Elements cannot be added at specific positions within a set.
Hash Tables
- Hash tables map keys to values via a hash function.
- Elements are grouped by shared characteristics.
- The hash code determines an element's position in the table.
- Elements cannot be added at specific positions.
Hash Codes
- Hash codes are numerical values, calculated for efficient indexing.
- They must be consistent and uniform.
- They cannot be directly used as array indexes (values might be too large).
Collisions
- Collisions happen when multiple elements hash to the same index.
- They are managed via chaining (linked lists) or probing.
Code Snippets
- Compressing a hash code to an array index:
int h = x.hashCode();
if (h < 0) { h = -h; }
position = h % arrayLength;
- Adding an element using chaining:
Node newNode = new Node();
newNode.data = x;
newNode.next = buckets[h];
buckets[h] = newNode;
Operations and Complexity
- Add: Average: O(1), Worst: O(n)
- Remove: Average: O(1), Worst: O(n)
- Search: Average: O(1), Worst: O(n)
Collision Handling
- Chaining: Uses linked lists (or other structures) at each index to handle multiple elements.
- Probing: Searches for the next available slot in the array:
- Linear Probing: Checks the next slot sequentially.
- Quadratic Probing: Checks further slots to reduce clustering.
- Double Hashing: Uses a second hash function for probe intervals.
Linear Probing Issues
- Clustering: Leads to long contiguous groups, increasing search times.
Hash Function Requirements
- Uniform Distribution: Evenly distributes keys across the table.
- Consistency: Returns the same hash for the same input.
- Deterministic Behavior: Output depends only on the input.
Key Definitions
- Bucket: A structure (like a linked list) used in chaining.
- Load Factor: The ratio of elements to table size.
- Formula: Load Factor = Number of Entries / Table Size.
- Example: 650/1500 = 0.433 (or 43.3%).
Special Techniques and Terms
- Re-hashing: Resizing the hash table and reassigning hash codes.
- Perfect Hash Function: Maps each key to a unique slot (possible only with all known keys).
- Key-Value Pair: The fundamental unit of a dictionary/map.
True/False Key Facts
- Statement 1: A perfect hash function maps each search key to a unique location. TRUE
- Statement 2: Double hashing significantly reduces clustering. TRUE
- Statement 3: Linear probing results in clustering. TRUE
- Statement 4: Quadratic probing completely eliminates clustering. FALSE
- Statement 5: Set difference is commutative. FALSE
- Statement 6: With separate chaining, a dictionary's size can exceed the hash table's size. TRUE
- Statement 7: The
getValue(searchKey)
method retrieves the search key for a given value. FALSE
Miscellaneous
- Other names for ADT Dictionary: Map, Associative Array.
- Map Ordering: Typically ordered by key.
- Set Operations:
- Union: Combines elements.
- Intersection: Finds common elements.
- Difference: Finds elements in one set but not the other.
Frequently Asked Questions
- Search Key Exists: Update the value associated, or reject the new entry.
- Primary Clustering: Causes groups of elements, increasing collisions.
- Collision: When two keys hash to the same index.
- Linear Probing: Searches sequentially for the next free slot.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.