Data Structures & AlgorithmsModerateTCS NQT · hashing
Problem: A hash table uses chaining, and every insertion first scans the key’s chain to make sure the key is not already present. What is the worst-case time of one insertion when all keys collide?
- A. O(1) per insertion
- B. O(n) per insertion
- C. O(log n) per insertion
- D. O(n^2) per insertion
Correct answer: B. O(n) per insertion
All keys hash to one bucket, so its chain holds up to n keys, and the duplicate check scans the whole chain before inserting: O(n). Inserting at the head without that check would be O(1), which is why the check is stated.
More Coding Interview Problems questions
- Problem: Which problem does the 'LCA by Euler tour and RMQ' reduce to?
- Problem: Which property does a 'matroid intersection' problem violate in general, making it harder than a single matroid?
- Problem: What is the time complexity of finding a minimum spanning tree in a complete graph with V vertices using Prim's algorithm and an…
- Problem: What does Fermat's little theorem let you compute efficiently?
- Problem: Which statement about the Bellman-Ford algorithm is correct when there is a negative cycle reachable from the source?
- Problem: What is the minimum number of colours needed to colour a bipartite graph so that no adjacent vertices share a colour?