Data Structure Quick Reference Flashcards

(18 cards)

1
Q

When to use a HashMap (Dictionary)?

A
  • Store key-value pairs
  • Count frequencies
  • Check existence in O(1)

Python syntax: Create: d = {} or d = defaultdict(int)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you add/update a value in a HashMap?

A
  • d[key] = value
  • d[key] = d.get(key, 0) + 1

This allows for both setting a value and incrementing a count.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a common use case for a HashMap?

A
  • Two Sum
  • Group Anagrams
  • Counting problems

These problems often require efficient key-value storage.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

When to use a Set?

A
  • Check uniqueness
  • Check presence in O(1)

Python syntax: Create: s = set()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you add an element to a Set?

A

s.add(element)

This method adds an element to the set if it is not already present.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a common use case for a Set?

A
  • Finding duplicates
  • Tracking seen elements in sliding window

Sets are useful for operations that require uniqueness.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

When to use a Stack (List)?

A
  • Need LIFO (Last In First Out)

Python syntax: Create: stack = []

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you push an element onto a Stack?

A

stack.append(element)

This adds an element to the top of the stack.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a common use case for a Stack?

A
  • Valid Parentheses
  • Monotonic stack problems

Stacks are often used in algorithms that require backtracking.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

When to use a Queue (Deque)?

A
  • Need FIFO (First In First Out)

Python syntax: Import: from collections import deque

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you enqueue an element in a Queue?

A

queue.append(element)

This adds an element to the end of the queue.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a common use case for a Queue?

A
  • BFS
  • Level-order traversal
  • Sliding window maximum

Queues are essential for breadth-first search algorithms.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

When to use a Heap (Priority Queue)?

A
  • Access min/max element repeatedly

Python syntax: Import: from heapq import heappush, heappop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you create a min heap?

A

heap = []

This initializes an empty list to be used as a min heap.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a common use case for a Heap?

A
  • Top K elements
  • Kth largest
  • Merge K sorted lists

Heaps are useful for problems that require efficient retrieval of the smallest or largest elements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

When to use a Counter?

A
  • Count frequencies of elements

Python syntax: Import: from collections import Counter

17
Q

How do you create a Counter from an array?

A

count = Counter(array)

This initializes a Counter object that counts the occurrences of each element in the array.

18
Q

What is a common use case for a Counter?

A
  • Top K Frequent Elements
  • Anagram problems

Counters simplify frequency counting tasks in various algorithms.