Interview conceptCoding & Algorithms

Hash Tables, Sets, And Frequency Counting

Asked of: Software Engineer

Last updated

Editorial hash-table diagram showing keys hashed into buckets, a collision chain, frequency counting, set membership, and expected versus worst-case lookup time.

What's being tested

These problems test hash-based lookup, frequency counting, deduplication, and deriving compact keys that avoid quadratic pairwise comparisons. You should connect the data structure choice to constraints, state average versus worst-case complexity, and handle ordering, duplicate, collision, and edge-case requirements precisely.

Patterns & templates

  • Frequency map — count occurrences with a dictionary; use parity (count % 2) for palindrome feasibility in O(n) time and O(k) space.
  • Set membership — track seen values or coordinate pairs for expected O(1) lookup; use a separate output set when duplicates must be returned once.
  • Derived-key grouping — replace pairwise comparisons with wildcard keys, such as removing one digit position; aggregate counts in O(nL) for n strings of length L.
  • Vertical symmetry — candidate mirror is minX + maxX; verify (2 * mirrorX - x, y) exists for every point, preserving multiplicity if points may repeat.
  • Hash-table mechanics — explain hashing, collisions via chaining or open addressing, load factor, resizing, and expected O(1) search versus O(n) worst case.
  • Ordering requirements — hash structures do not guarantee sorted output; sort distinct results afterward, costing O(d log d) for d returned values.

Common pitfalls

Pitfall: Checking only whether a value exists misses frequency requirements; distinguish “appears more than once” from “appears at least once.”

Pitfall: Claiming hash lookup is always O(1) ignores collisions, adversarial inputs, and resizing; say expected O(1) with O(n) worst case.

Pitfall: Generating every pair of strings or points creates O(n²) work; derive keys or use complementary lookups instead.

Practice these

The practice cards below cover the canonical variants — solve all of them and time yourself.

Practice questions

Related concepts