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)?

  1. 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
  2. B. Both are always O(1)
  3. C. A hash set is always O(log n) too
  4. 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

Exams that ask this topic