Coding Patterns Flashcards

(26 cards)

1
Q

Front

A

Back

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

Two Pointers (Pattern)

A

Uses two indices to traverse a data structure, often from opposite ends or at different speeds.

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

When to use Two Pointers

A

Finding pairs/triplets in sorted arrays, palindrome checks, removing duplicates. Optimizes from O(n²) to O(n).

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

Sliding Window (Pattern)

A

A contiguous sub-array/sub-string that “slides” (expands and contracts) across the data.

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

When to use Sliding Window

A

Finding longest/shortest contiguous substring/subarray, or max/min sum of a fixed size.

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

Prefix Sums (Pattern)

A

An array where each element is the cumulative sum (or product) up to that index.

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

When to use Prefix Sums

A

Quickly calculating the sum of multiple sub-ranges in O(1) time after O(n) preprocessing.

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

Cyclic Sort (Pattern)

A

In-place sort for arrays containing numbers in a specific range (e.g., 1 to N).

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

When to use Cyclic Sort

A

Finding missing numbers, duplicates, or corrupted numbers in a 1-to-N range array.

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

Fast & Slow Pointers (Pattern)

A

Uses two pointers (tortoise and hare) that move at different speeds.

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

When to use Fast & Slow Pointers

A

Detecting a cycle in a linked list, finding the middle element, or finding a “Happy Number”.

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

In-place Reversal (Pattern)

A

Reversing the links between nodes in a linked list without using extra space.

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

When to use In-place Reversal

A

Reversing a linked list or a sub-list.

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

Breadth-First Search (BFS)

A

Traverses a tree or graph level by level using a Queue.

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

When to use BFS

A

Finding the shortest path in an unweighted graph, level order traversal of a tree.

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

Depth-First Search (DFS)

A

Explores as far as possible along each branch before backtracking, using recursion or a Stack.

17
Q

When to use DFS

A

Finding a path, checking connectivity, tree traversals (in-order, pre-order, post-order).

18
Q

Backtracking (Pattern)

A

A systematic way to try all possible configurations, “backtracking” when a path is invalid.

19
Q

When to use Backtracking

A

Problems involving permutations, combinations, or subsets (e.g., N-Queens, Sudoku).

20
Q

Binary Search (Pattern)

A

Efficiently finds an element by repeatedly dividing the search interval in half.

21
Q

When to use Binary Search

A

Searching in a sorted array or a sorted search space, in O(log n) time.

22
Q

Two Heaps (Pattern)

A

Uses a Max Heap and a Min Heap to divide a set into two halves.

23
Q

When to use Two Heaps

A

Finding the median of a stream of numbers.

24
Q

Top ‘K’ Elements (Pattern)

A

Finds the K largest, smallest, or most frequent elements, often using a Min/Max Heap.

25
Merge Intervals (Pattern)
A technique to handle overlapping intervals by sorting and merging them.
26
Dynamic Programming (DP)
Solves complex problems by breaking them into simpler, overlapping subproblems and storing their results.