Data Structures & AlgorithmsHardInfosys SP/DSE · Product Company SDE
Problem: Which correctly describes why a hash set gives O(1) average-case membership testing, but a balanced BST set gives O(log n)?
- A. A hash set computes a hash and does O(1) average bucket lookup (ignoring collisions), while a BST must compare against O(log n) nodes along a root-to-leaf path to narrow down the search
- B. Both are always O(1)
- C. A hash set is always O(log n) too
- D. A BST set is faster because it avoids hashing overhead
Correct answer: A. A hash set computes a hash and does O(1) average bucket lookup (ignoring collisions), while a BST must compare against O(log n) nodes along a root-to-leaf path to narrow down the search
Hashing jumps (on average) directly to the right bucket in O(1), while a balanced BST narrows the search by a constant fraction at each of its O(log n) levels; the BST trades some speed for maintaining sorted order, which a hash set does not provide.
More Coding Interview Problems questions
- Problem: Which of these correctly explains why BFS (not DFS) is used to find the shortest path in an unweighted graph?
- Problem: What is the time complexity of detecting a cycle in an undirected graph using Union-Find, processing E edges?
- Problem: A recursive solution explores every possible way to partition a string into palindromic substrings (palindrome partitioning)…
- Problem: Which best describes the relationship between the number of valid Binary Search Trees on n distinct keys and the Catalan numbers?
- Problem: What is the time complexity of the standard iterative binary search, and what precondition must hold for it to work correctly?
- Problem: Which best describes why DFS-based topological sort (using finish times, pushing to a stack) correctly produces a topological…