π Hash table Implementation in Directory (10 MCQs)
Hash-Based Filename Lookup in Directories
Hash-table directory implementation uses a hash function to convert filenames into indexes that identify locations within a directory search structure. Instead of examining every entry sequentially, the operating system calculates a hash value and accesses the corresponding bucket. This approach accelerates filename lookup in large directories, although collisions and table maintenance require additional mechanisms.
Why Sequential Directory Searching Becomes Expensive
Linear-list searching may require examining every directory entry, creating delays as the number of files increases. Hashing provides faster expected lookup by distributing filenames across multiple buckets. However, different filenames can produce the same hash value, so the implementation must resolve collisions and preserve correct name comparisons.
Hash Calculation and Collision Resolution
When a file is requested, the directory implementation computes its hash value and selects the corresponding bucket. It searches the entries stored in that bucket and compares actual filenames to confirm a match. Collisions can be handled through chaining or another suitable method. Insertion and deletion update the relevant bucket and any associated directory metadata.
Locating a File Through a Hashed Directory
Suppose a directory uses 100 buckets and the filename report.txt produces hash index 27. The operating system searches bucket 27 rather than scanning all directory entries. If another filename maps to the same bucket, both entries are examined until the correct name is found. This reduces expected search work when the hash function distributes names effectively.
Expected Lookup Speed and Collision Overhead
Hash-table directories provide efficient expected lookup and are suitable for large file collections. Their performance depends on hash-function quality, bucket count, and load factor. Heavy collisions can increase search time toward linear behaviour. Resizing or rehashing also requires additional work, and directory implementations must preserve correct handling of duplicate names and concurrent modifications.
Common Misconceptions About Hash-Based Directories
'A hash value uniquely identifies every filename' is incorrect because collisions are possible. 'Hash-table lookup always takes exactly one operation' is incorrect because collisions and filename verification add work. 'Hashing automatically stores directory entries in alphabetical order' is incorrect because bucket placement does not preserve lexical ordering.
Exam Focus: Bucket Calculation and Collision Handling
Calculate bucket indexes using a supplied hash function and trace the lookup process. Explain collision resolution through chaining or another specified method. Compare expected hash-table performance with linear-list searching. Questions may also examine load factor, table resizing, duplicate-name detection, and why a hash table does not naturally support ordered directory traversal.
Key Terms of Hash-Table Directories
- Hash function - Converts filenames into indexes.
- Bucket - Stores matching hash entries.
- Collision - Occurs when hashes share an index.
- Chaining - Links entries within a bucket.
- Load factor - Ratio of entries to buckets.
- Rehashing - Rebuilds the hash structure.
π All Hash table Implementation in Directory MCQs
Q1. What is the primary purpose of a hash function in a hash table?
π Explanation: A hash function takes an input key and produces a numeric value that is used as an index into the table. This index determines where the associated record is stored, allowing nearβconstantβtime lookup, insertion, and deletion. Options A, C, and D describe unrelated operations: sorting, encryption, and compression are not the role of a hash function in a hash table. By converting keys directly to positions, the function enables the core advantage of hash tablesβfast access.
Q2. Why does a high load factor generally increase the average search time in a hash table?
π Explanation: A high load factor means many keys share the same limited number of buckets, so collisions become more frequent. Each collision forces the algorithm to examine additional entriesβeither by following a chain or probing furtherβraising the average number of steps required to find or insert a key. Options A and D describe effects that are not directly tied to load factor, while option C is incorrect because a high load factor does not trigger automatic expansion without explicit resizing logic.
Q3. A hash table uses separate chaining with 10 buckets. If 30 keys are inserted, what is the expected average chain length?
π Explanation: The average chain length equals the load factor, calculated as the number of keys divided by the number of buckets. With 30 keys and 10 buckets, the load factor is , meaning each bucket will on average hold three keys in its linked list. Option B underestimates the length, option C overestimates it, and option D suggests an unrealistically short chain. Understanding this relationship helps predict memory usage and lookup cost in a chained hash table.
Q4. How does double hashing improve upon linear probing in open addressing?
π Explanation: Double hashing generates a second hash value that determines the step size for each probe, so the sequence of examined slots depends on both hash results. This breaks up the contiguous clusters that linear probing often creates, leading to more uniform probe distribution and fewer repeated collisions. Options A, B, and C describe unrelated benefits; they do not address the core issue of probe pattern and clustering that double hashing specifically mitigates.
Q5. In a dynamic hash table that doubles its size when a threshold is crossed, which condition typically triggers the rehash operation?
π Explanation: Most implementations monitor the load factor, defined as where is the number of stored keys and is the bucket count. When this ratio grows beyond a preset limitβcommonly 0.75βthe table is resized (usually doubled) and all keys are reβhashed to maintain efficient O(1) operations. Option A concerns deletions, option C would cause excessive rehashing, and option D is irrelevant because an empty table does not need resizing. Keeping the load factor below the threshold preserves performance.
Q6. What is the main advantage of choosing a prime number as the size of a hash table when using the division method?
π Explanation: When the table size is prime, the modulo operation in the division method () tends to distribute keys more uniformly because fewer keys share common factors with the table size. This reduces systematic clustering that can arise with composite sizes, especially when keys have patterns related to those factors. Option A is true but not the key advantage, option B is falseβcollisions can still occur, and option D is unrelated to hash distribution.
Q7. Given the hash function and a table of size 13 using linear probing, where will key 9 be placed if slots 8 and 9 are already occupied?
π Explanation: First compute the primary index: . Because slot 8 is occupied, linear probing checks the next slot, 9, which is also occupied. The algorithm then proceeds to slot 10, which is free, so key 9 is stored there. Options A, B, and C refer to either the original hash index or already occupied slots, making them incorrect.
Q8. Why might a hash table with open addressing degrade to performance even if the load factor is kept below 0.5?
π Explanation: If the hash function does not spread keys uniformly, many of them will hash to a narrow region of the table, producing primary or secondary clustering. Even with a low load factor, probes must scan long sequences of occupied slots, turning average lookup time into linear behavior. Options B and C describe unrelated hardware constraints, and option D is incorrect because the scenario specifies open addressing, not chaining.
Q9. A system uses a hash table with separate chaining to cache file descriptors. If 2000 descriptors are stored in a table with 1024 buckets, what is the expected average chain length?
π Explanation: The average chain length equals the load factor, calculated as . This means each bucket will on average contain about two descriptor entries, indicating moderate memory overhead and lookup cost. Option A is far too low, option C overestimates the average, and option D suggests an even higher chain length. Knowing this metric helps system designers balance memory usage against lookup efficiency in descriptor caching.
Q10. A cache uses quadratic probing with a table size of 11. The hash function gives an initial index of 4 for a key. The first probe () checks and finds it occupied. The second probe () checks which is free. Where will the key be placed?
π Explanation: Quadratic probing computes the next index as . Starting from 4, the first probe (i=1) leads to slot 5, which is taken, so the algorithm continues. The second probe (i=2) yields , which is empty, so the key is stored at slot 8. Option A stops prematurely, option C ignores probing, and option D does not follow the quadratic formula, making them incorrect.