Interview's MateDSA Visualizer
Interview's Mate

DSA Visualizer

Learn by watching algorithms run

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
All topics
DATA STRUCTURES

Hash Maps

Key-value lookups in O(1) on average - the fastest way to remember what you’ve seen.

Beginner11 problems3 interactive subtopicsO(n) time · O(n) space
Practice in AI DSA
WalkthroughOverviewRecognize itMistakes & edge casesKnowledge check (3)Practice
Algorithms & custom examples (3)Open in full playground →
STEP 1/4Start with an empty map
target = 6
012
3
2
4
SEEN (VALUE: INDEX)
(empty)
  • active this step
  • visited / done
  • result
Space Play←→ StepR Replay
1 / 4
PYTHONnums = [3, 2, 4], target = 6
1def two_sum(nums, target):
▶2 seen = {}
3 for i, x in enumerate(nums):
4 need = target - x
5 if need in seen:
6 return [seen[need], i]
7 seen[x] = i
8 return []
STEP 1 OF 4Setup

Start with an empty map

seen will map each value to its index.

VARIABLES

i
-
need
-
seen
{}
02

Intuition & complexity

The core mental model, trade-offs, and Big-O cost.

What it is

A hash map (a Python dict) stores key-value pairs and finds a key by hashing it to a bucket. A set is the keys-only version.

Why it matters

Spending O(n) memory for O(1) lookups turns many O(n²) searches into a single pass.

Think of it like this

A coat check: hand over your ticket number and get your coat back without searching every hanger.

Use it when

  • Counting frequencies
  • Finding a complement or pair in one pass
  • Grouping items by a computed key

Avoid it when

  • You need keys in sorted order - use a sorted list or a tree
  • Memory is extremely limited

Complexity cheatsheet

OperationTimeSpace
Insert, lookup or deleteO(1) averageO(n) total
Worst case (many collisions)O(n)-
Iterate over all keysO(n)O(1)

Often confused with: two pointers - solves pair sums with O(1) memory, but needs sorted input.

03

How to recognize it

Clues in interview prompts that point straight to this pattern.

countfrequencypair that sums toseen beforegroup by
  • “Return the indices” plus unsorted input points to a dict of value to index
  • Lists can’t be keys; convert them to tuples
04

Mistakes & edge cases

Catch common off-by-one bugs and corner cases before submitting.

Common mistakes

  • Storing x before checking - 3 + 3 = 6 would wrongly return [0, 0]
  • Using a list for membership checks (O(n) each)
  • Assuming dict order means sorted order - it’s insertion order

Edge cases to test

  • Duplicates like [3, 3] with target 6
  • Negative numbers and zero
  • No valid pair
Interview tip: Say “O(1) on average” - it shows you know the worst case is O(n).
05

Knowledge check

Quick check to lock in the intuition.

Question 1 of 3

For nums = [3, 3] and target 6, why check the map before storing?

06

Practice

Apply what you just watched on real interview problems.

Generate Hash Table problem in AI DSA Practice →
  • BeginnerTwo SumLeetCode 1 ↗ (opens in a new tab)
  • Intermediate4Sum IILeetCode 454 ↗ (opens in a new tab)
  • AdvancedLRU CacheLeetCode 146 ↗ (opens in a new tab)
QueuesTrees