Arrays & Hashing

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall with Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/45

flashcard set

Earn XP

Description and Tags

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No study sessions yet.

46 Terms

1
New cards

What is the main idea of hashing problems?

Use a hash structure (set or map) to get O(1) average lookup instead of scanning the array

2
New cards

General hashing problem skeleton

"Initialize hash structure โ†’ iterate โ†’ check condition โ†’ update hash โ†’ return result"

3
New cards

Python HashSet creation

set() or set(nums)

4
New cards

Python HashMap creation

{} or dict()

5
New cards

Python frequency map template

"from collections import defaultdict\nfreq = defaultdict(int)\nfor x in nums:\n freq[x] += 1"

6
New cards

Python Counter usage

"from collections import Counter\nfreq = Counter(nums)"

7
New cards

When should you use a HashSet?

When you only care about existence and uniqueness

8
New cards

When should you use a HashMap?

When you need key โ†’ value relationships

9
New cards

Time complexity of HashSet operations

Average O(1) lookup insert delete

10
New cards

Worst case time complexity of HashSet operations

O(n) due to collisions

11
New cards

Time complexity of HashMap operations

Average O(1)

12
New cards

Worst case time complexity of HashMap operations

O(n)

13
New cards

Space complexity of hashing problems

O(n)

14
New cards

LC 217 โ€“ Contains Duplicate: check if any value appears twice

Pattern: HashSet โ€” store seen elements and check membership

15
New cards

LC 242 โ€“ Valid Anagram: check if two strings have same character counts

Pattern: Frequency HashMap or Counter

16
New cards

LC 1 โ€“ Two Sum: find two numbers that add up to target

Pattern: HashMap โ€” store complement

17
New cards

LC 49 โ€“ Group Anagrams: group words with same character frequency

Pattern: HashMap with sorted string or frequency tuple as key

18
New cards

LC 347 โ€“ Top K Frequent Elements: return k most frequent values

Pattern: HashMap + Heap or Bucket Sort

19
New cards

LC 238 โ€“ Product of Array Except Self: multiply all except self without division

Pattern: Prefix + Suffix arrays

20
New cards

LC 36 โ€“ Valid Sudoku: check if rows columns and boxes are valid

Pattern: HashSet tracking

21
New cards

LC 128 โ€“ Longest Consecutive Sequence: find longest run of consecutive numbers

Pattern: HashSet + greedy expansion

22
New cards

How does Two Sum hashing logic work?

For each num check if target-num exists in map else store num:index

23
New cards

Two Sum Python template

"seen = {}\nfor i

24
New cards

Valid Anagram Python template

"from collections import Counter\nreturn Counter(s) == Counter(t)"

25
New cards

Contains Duplicate Python template

"seen = set()\nfor n in nums:\n if n in seen: return True\n seen.add(n)\nreturn False"

26
New cards

Group Anagrams Python template

"from collections import defaultdict\ngroups = defaultdict(list)\nfor s in strs:\n key = ''.join(sorted(s))\n groups[key].append(s)\nreturn list(groups.values())"

27
New cards

Product Except Self idea

Prefix product * Suffix product

28
New cards

Product Except Self Python template

"res = [1]*n\nprefix = 1\nfor i in range(n):\n res[i] = prefix\n prefix *= nums[i]\nsuffix = 1\nfor i in range(n-1

29
New cards

Longest Consecutive core idea

Start only from numbers that have no left neighbor

30
New cards

Longest Consecutive Python template

"numSet = set(nums)\nlongest = 0\nfor n in numSet:\n if n-1 not in numSet:\n length = 1\n while n+length in numSet:\n length += 1\n longest = max(longest

31
New cards

Valid Sudoku checking idea

Use 3 hash sets: rows cols boxes

32
New cards

Valid Sudoku Python structure

"rows = [set() for _ in range(9)]\ncols = [set() for _ in range(9)]\nboxes = [set() for _ in range(9)]"

33
New cards

What is the biggest advantage of hashing?

Constant time lookups

34
New cards

Common mistake in hashing problems

Forgetting to update the hash after checking

35
New cards

Common mistake in Two Sum

Storing before checking

36
New cards

Common mistake in frequency problems

Not initializing counts correctly

37
New cards

How do you avoid KeyError in Python dict?

Use get() or defaultdict

38
New cards

dict.get() usage

dict.get(key

39
New cards

defaultdict benefit

Automatically initializes missing keys

40
New cards

When NOT to use hashing?

When order matters or memory is constrained

41
New cards

How do you check membership in O(1)?

Using a set

42
New cards

How do you track frequency?

Using a dictionary or Counter

43
New cards

Why is hashing good for duplicate detection?

Fast existence checking

44
New cards

Key idea of Group Anagrams

Same frequency signature

45
New cards

Key idea of Longest Consecutive

Only start sequences from valid starts

46
New cards

Key idea of Product Except Self

Prefix and suffix multiplication

Explore top flashcards

October exam
Updated 465d ago
flashcards Flashcards (32)
10/6
Updated 218d ago
flashcards Flashcards (62)
PSCH 262 Final Exam
Updated 634d ago
flashcards Flashcards (110)
WWII
Updated 4d ago
flashcards Flashcards (35)
October exam
Updated 465d ago
flashcards Flashcards (32)
10/6
Updated 218d ago
flashcards Flashcards (62)
PSCH 262 Final Exam
Updated 634d ago
flashcards Flashcards (110)
WWII
Updated 4d ago
flashcards Flashcards (35)