When to use a HashMap (Dictionary)?
Python syntax: Create: d = {} or d = defaultdict(int)
How do you add/update a value in a HashMap?
This allows for both setting a value and incrementing a count.
What is a common use case for a HashMap?
These problems often require efficient key-value storage.
When to use a Set?
Python syntax: Create: s = set()
How do you add an element to a Set?
s.add(element)
This method adds an element to the set if it is not already present.
What is a common use case for a Set?
Sets are useful for operations that require uniqueness.
When to use a Stack (List)?
Python syntax: Create: stack = []
How do you push an element onto a Stack?
stack.append(element)
This adds an element to the top of the stack.
What is a common use case for a Stack?
Stacks are often used in algorithms that require backtracking.
When to use a Queue (Deque)?
Python syntax: Import: from collections import deque
How do you enqueue an element in a Queue?
queue.append(element)
This adds an element to the end of the queue.
What is a common use case for a Queue?
Queues are essential for breadth-first search algorithms.
When to use a Heap (Priority Queue)?
Python syntax: Import: from heapq import heappush, heappop
How do you create a min heap?
heap = []
This initializes an empty list to be used as a min heap.
What is a common use case for a Heap?
Heaps are useful for problems that require efficient retrieval of the smallest or largest elements.
When to use a Counter?
Python syntax: Import: from collections import Counter
How do you create a Counter from an array?
count = Counter(array)
This initializes a Counter object that counts the occurrences of each element in the array.
What is a common use case for a Counter?
Counters simplify frequency counting tasks in various algorithms.