TRT
Sign in
easyCoreconcept

When would you use a binary search tree instead of a hash table?

Compare costs, guarantees and supported operations, and name the library types that implement each.

TRT practice prompt — not a verified question from a named employer.

The Runtime Theory Team2 min read
Learn it firstBinary search treesPro — members onlyA binary search tree keeps smaller keys to the left of each node and larger keys to the right, so a search discards a whole subtree at every step. Its speed depends entirely on its height.

A strong answer

A strong candidate frames When would you use a binary search tree instead of a hash table? around the exact memory layout, compiler semantics, and hardware execution model covered in Binary search trees. Compare costs, guarantees and supported operations, and name the library types that implement each.

A binary search tree (BST) is a binary tree whose nodes hold keys arranged by one rule: for every node, every key in its left subtree is smaller than the node's key, and every key in its right subtree is larger. The rule applies to whole subtrees, not just to a node's children, which is the detail most often gotten wrong when checking a tree.

That rule turns the tree into a decision procedure. Standing at any node, a single comparison with the key you are looking for tells you which subtree it must be in, and rules out the other subtree completely, however many keys it holds. It is binary search with the array replaced by a structure of nodes.

In a production interview, always connect the specification rule to its concrete runtime consequence: state what lives on the stack versus the heap or static storage segment, how the compiler or operating system validates alignments, lifetimes, and error codes, and what happens under adversarial or boundary inputs (such as zero-length buffers, integer overflow, concurrent mutation, or partial I/O). Backing your explanation with a small trace of register/memory state transitions immediately separates senior systems engineers from candidates who only memorized syntax.

What the interviewer is listening for

  • Mastery of core invariant: State the binary search tree property and check it correctly.
  • Mastery of core invariant: Search for a key, find the minimum and maximum, and find a key's successor.
  • Mastery of core invariant: Explain why every operation costs O(height), and how the order of insertions sets the height.
  • Hardware and ABI awareness: Explaining how sizes, alignments, calling conventions, or kernel boundaries behave on real 64-bit targets rather than relying on implementation-defined assumptions.
  • Defensive engineering: Naming the exact compiler flags (-Wall -Wextra -Werror), static analyzers, or runtime sanitizers (-fsanitize=address,undefined,thread) that verify correctness in CI.

Common mistakes

  • Explaining only surface syntax without tracing the underlying memory addresses, ownership lifetime, or kernel/user-space state transitions.
  • Assuming undefined behavior (such as signed overflow, uninitialized reads, strict-aliasing violations, or dangling pointers) will "just work" because it passed an unoptimized -O0 debug build.
  • Ignoring return codes, partial reads/writes, alignment padding, or cleanup paths when an allocation or syscall fails midway through a function.

This answer walks

Practice follow-ups

  1. 01How would you find all keys between 100 and 200 with each structure?
  2. 02What happens to an unbalanced BST if keys arrive in sorted order?
  3. 03Which structure would you use to implement a leaderboard, and why?
  4. 04When is a sorted array better than both?

Not started

Sign in to save your learning progress.

Sign in to save