Algo Patterns Flashcards

(29 cards)

1
Q

You need to find a pair of elements in a sorted array that sum to a target. What pattern?

A

Two Pointers - use one pointer at start, one at end, move them based on whether current sum is too small or too large. O(n) time, O(1) space.

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

You need to find the longest substring without repeating characters. What pattern?

A

Sliding Window - expand window by moving right pointer, shrink by moving left pointer when you see a duplicate. Track characters in a Set or Map.

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

You need to detect if a linked list has a cycle. What pattern and algorithm?

A

Fast and Slow Pointers (Floyd’s Cycle Detection) - slow moves 1 step, fast moves 2 steps. If they meet, there’s a cycle. O(n) time, O(1) space.

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

You need to find the middle of a linked list in one pass. What pattern?

A

Fast and Slow Pointers - when fast reaches the end, slow is at the middle. Fast moves 2 steps, slow moves 1 step.

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

You need to merge overlapping intervals. What pattern?

A

Merge Intervals - sort by start time, then iterate and merge if current interval overlaps with previous. O(n log n) due to sorting.

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

You need to find if an array contains duplicates within k indices of each other. What pattern?

A

Sliding Window with a Set - maintain a window of size k, check if new element exists in window before adding.

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

You need to find all permutations of a string or array. What pattern?

A

Backtracking - build solutions incrementally, abandon partial solutions that can’t lead to valid results. Make a choice, recurse, undo the choice.

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

You need to find all subsets of a set. What pattern?

A

Backtracking or iterative bit manipulation. For each element, choose to include or exclude it. 2^n subsets total.

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

You need to find the shortest path in an unweighted graph. What algorithm?

A

BFS (Breadth-First Search) - explores all neighbors at current depth before moving deeper. Guarantees shortest path in unweighted graphs.

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

You need to find the shortest path in a weighted graph with non-negative weights. What algorithm?

A

Dijkstra’s Algorithm - use a priority queue, always process the vertex with smallest known distance. O((V + E) log V) with binary heap.

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

You need to find the shortest path in a weighted graph that may have negative weights. What algorithm?

A

Bellman-Ford Algorithm - relax all edges V-1 times. Can detect negative cycles. O(V * E) time.

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

You need to find if a path exists between two nodes in a graph. What algorithms can you use?

A

BFS or DFS. BFS finds shortest path in unweighted graphs; DFS uses less memory (O(depth) vs O(width)).

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

You need to determine order of tasks given dependencies. What algorithm?

A

Topological Sort - only works on DAGs. Use Kahn’s algorithm (BFS with in-degree tracking) or DFS with post-order processing.

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

How do you detect a cycle in a directed graph?

A

During DFS, track nodes in current recursion stack. If you visit a node already in the stack, there’s a cycle. Or use topological sort - if not all nodes are processed, cycle exists.

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

You need to find the kth largest element in an array. What are two approaches?

A

1) Min-heap of size k: O(n log k). 2) QuickSelect: O(n) average, O(n²) worst case. Heap is better when k «_space;n or for streaming data.

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

You need to merge k sorted lists. What pattern?

A

K-way Merge with a min-heap - add first element of each list to heap, extract min, add next element from that list. O(n log k).

17
Q

You need to find an element in a sorted array. What algorithm?

A

Binary Search - O(log n). Compare target to middle element, eliminate half of remaining elements each iteration.

18
Q

You need to find the first occurrence of an element in a sorted array with duplicates. What modification to binary search?

A

When you find target, don’t return immediately. Instead, continue searching in left half (set right = mid - 1). Return the leftmost found index.

19
Q

You need to find the minimum in a rotated sorted array. What approach?

A

Modified Binary Search - compare mid to right. If mid > right, minimum is in right half. If mid <= right, minimum is in left half (including mid).

20
Q

You need to solve a problem optimally where the solution depends on solutions to subproblems. What pattern?

A

Dynamic Programming - identify overlapping subproblems and optimal substructure. Memoize or tabulate results.

21
Q

What are the two approaches to implementing dynamic programming?

A

Top-down (memoization): recursive with caching. Bottom-up (tabulation): iterative, fill table from base cases up.

22
Q

You need to count the number of ways to climb n stairs taking 1 or 2 steps at a time. What pattern?

A

Dynamic Programming (Fibonacci-style). dp[i] = dp[i-1] + dp[i-2]. Can optimize to O(1) space by keeping only last two values.

23
Q

You need to find the longest common subsequence of two strings. What pattern?

A

Dynamic Programming with 2D table. dp[i][j] = longest common subsequence of first i chars of s1 and first j chars of s2.

24
Q

You have items with weights and values, and a capacity limit. How do you maximize value? What pattern?

A

0/1 Knapsack - Dynamic Programming. dp[i][w] = max value using first i items with capacity w. For each item: include it or don’t.

25
You need to find connected components in an undirected graph. What approaches?
BFS/DFS from each unvisited node, or Union-Find. Each complete traversal finds one connected component.
26
What is Union-Find (Disjoint Set Union) used for?
Efficiently tracking connected components and determining if two elements are in the same group. With path compression and union by rank: nearly O(1) per operation.
27
You need to find if you can partition an array into two subsets with equal sum. What pattern?
Dynamic Programming (subset sum variant). Create boolean dp[sum/2+1] where dp[i] indicates if subset summing to i is possible.
28
You need to find the minimum number of coins to make a given amount. What pattern?
Dynamic Programming (unbounded knapsack variant). dp[amount] = min coins needed. For each coin, dp[i] = min(dp[i], dp[i-coin] + 1).
29
When should you use BFS vs DFS for tree/graph traversal?
BFS: shortest path in unweighted graph, level-order traversal, closer solutions first. DFS: detecting cycles, topological sort, uses less memory, exploring all paths.