Hash Tables

Key-to-value lookup in average O(1) via hashing into buckets.

Hash Tables Practice Problems

Easy

4 problems
  1. 380

    Valid Anagram

    Decide whether two words use exactly the same letters by counting the letters of one in a hash map and spending them on the other.

    easy
  2. 381

    Contains Duplicate

    Answer whether any value repeats in an array by keeping every value seen so far in a hash set and stopping at the first repeat.

    easy
  3. 382

    Ransom Note

    Check whether one string can be built from the letters of another by counting the available letters in a hash map and spending them.

    easy
  4. 383

    First Unique Character in a String

    Find the index of the first character that never repeats, using one pass to count every character and a second pass to find it.

    easy

Medium

4 problems
  1. 384

    Group Anagrams

    Group words that are rearrangements of each other by filing every word in a hash map under the sorted letters that form its signature.

    medium
  2. 385

    Valid Sudoku

    Check a sudoku board for repeated digits in any row, column or 3 by 3 box by storing one combined key per rule in a hash set.

    medium
  3. 386

    Design HashMap

    Build a hash map from scratch with put, get and remove, using a fixed set of buckets and a list in each bucket to handle collisions.

    medium
  4. 387

    Number of Boomerangs

    Count ordered triples of points where two of them sit the same distance from the third, by tallying squared distances per anchor point.

    medium

Hard

2 problems
  1. 388

    Max Points on a Line

    Find the most points sharing one straight line by tallying reduced direction keys in a hash map for every point taken as the anchor.

    hard
  2. 389

    Insert Delete GetRandom O(1)

    Support insert, remove and a fair random pick in constant time by pairing an array of values with a hash map from each value to its position.

    hard