Hash Tables, Sets, And Frequency Counting
Asked of: Software Engineer
Last updated

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 inO(n)time andO(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)fornstrings of lengthL. - 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 versusO(n)worst case. - Ordering requirements — hash structures do not guarantee sorted output; sort distinct results afterward, costing
O(d log d)fordreturned 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 expectedO(1)withO(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
- Find the common strings of two lists with the smallest index sumGeneral · Software Engineer · Other · easy
- Parse Log Entries and Group Messages by Level in Timestamp OrderGeneral · Software Engineer · Other · easy
- Find the second most frequent number in each matrix row, breaking frequency ties by larger valueGeneral · Software Engineer · Other · easy
- Explain how hash tables work and the average time complexity of searching themGeneral · Software Engineer · Other · easy
- Check Whether Any Rearrangement of a String Can Form a PalindromeGeneral · Software Engineer · Other · easy
- Check whether a set of 2D points has a vertical line of symmetryGeneral · Software Engineer · Other · medium
- Count pairs of equal-length digit strings that differ in exactly one positionGeneral · Software Engineer · Other · medium
- Find two-sum indices and a key-sorted frequency count of an integer arrayGeneral · Software Engineer · Other · easy
- Find the first character in reading order that repeats anywhere in a stringGeneral · Software Engineer · Other · easy
- Find every value that appears more than once in an integer array, sorted ascendingGeneral · Software Engineer · Other · easy
Related concepts
- Hash Map Counting And Frequency AnalysisCoding & Algorithms
- Hash Maps, Indexes, And Mutable State ModelingCoding & Algorithms
- Arrays, Strings, Hash Maps, And Frequency CountingCoding & Algorithms
- Arrays, Strings, Hashing, And Two PointersCoding & Algorithms
- Arrays, Strings And Hash CountingCoding & Algorithms
- Hash-Based Counting And CanonicalizationCoding & Algorithms