1/31
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
Counter (collections)
Use when you need frequency counts or histograms. Built in O(n). Common for anagrams, duplicates, most common elements.
Counter Tradeoff
Extra memory for unique keys. Not ordered. Cleaner than manual dict counting.
defaultdict(list)
Use when grouping items by a key or building adjacency lists. O(1) per append.
defaultdict(list) Tradeoff
Automatically creates keys on access, which can hide bugs. Slight overhead vs dict.
deque (collections)
Use when adding/removing from both ends is required. Ideal for queues, BFS, sliding windows.
deque Tradeoff
No fast random access. Overkill if only one end is used.
heapq
Use when you must repeatedly find min or max efficiently. O(log n) per push/pop.
heapq Tradeoff
Not fully sorted. No fast arbitrary removal. Max-heap requires negation trick.
Problem says 'top K'
Use heapq to maintain K best elements instead of sorting everything.
Problem says 'group by category'
Use defaultdict(list) to map keys to lists.
Problem says 'count frequency'
Use Counter for clean and fast counting.
Problem says 'sliding window'
Use deque for O(1) window updates.
defaultdict
Use when grouping items into categories (e.g., grouping Jira tasks by Status) to avoid KeyError and manual initialization.
Counter
Use when you need to tally frequencies and immediately find the 'Top K' most frequent elements.
deque
Use for BFS (Breadth-First Search) or any scenario requiring O(1) additions/removals from the FRONT of a list.
heapq
Use when you have a dynamic stream of data and always need instant access to the minimum or maximum value.
set
Use for O(1) membership checks (e.g., 'Have I visited this node?') or to instantly remove duplicates.
defaultdict(list) vs dict
defaultdict(list) removes the need for 'if key not in d: d[key] = []', making code cleaner and faster to write.
deque(maxlen=N)
A specialized version of deque that automatically 'evicts' the oldest item when a new one is added beyond capacity N.
heapq (Max-Heap)
Python's heapq is a Min-Heap by default; to use it as a Max-Heap, multiply your values by -1 when pushing and popping.
Counter.most_common(k)
The specific method used to return a list of the 'k' most frequent elements and their counts.
Standard List (pop(0))
NEVER use this for removing the first item in an interview; it is O(N). Use deque.popleft() which is O(1).
defaultdict
Use when grouping items into categories (e.g., grouping Jira tasks by Status) to avoid KeyError and manual initialization.
Counter
Use when you need to tally frequencies and immediately find the 'Top K' most frequent elements.
deque
Use for BFS (Breadth-First Search) or any scenario requiring O(1) additions/removals from the FRONT of a list.
heapq
Use when you have a dynamic stream of data and always need instant access to the minimum or maximum value.
set
Use for O(1) membership checks (e.g., 'Have I visited this node?') or to instantly remove duplicates.
defaultdict(list) vs dict
defaultdict(list) removes the need for 'if key not in d: d[key] = []', making code cleaner and faster to write.
deque(maxlen=N)
A specialized version of deque that automatically 'evicts' the oldest item when a new one is added beyond capacity N.
heapq (Max-Heap)
Python's heapq is a Min-Heap by default; to use it as a Max-Heap, multiply your values by -1 when pushing and popping.
Counter.most_common(k)
The specific method used to return a list of the 'k' most frequent elements and their counts.
Standard List (pop(0))
NEVER use this for removing the first item in an interview; it is O(N). Use deque.popleft() which is O(1).