DSA Patterns Flashcards

1
Q

How to identify the sliding window problem

A

You might be asked to find the longest or shortest substring, subarray or a certain value. Additionally, if the problem input is a linear data structure such as a linked list, array or string, chances are it is a sliding window problem.

Some common problems with the sliding window problem:

Maximum sum subarray of size “K”
Longest substring with “K” distinct characters
String anagrams

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

How to identify the two-pointer method

A

A two-pointer problem will feature sorted arrays or linked lists. You will typically need to find a set of elements that fulfill certain constraints. The set of elements in the array may also be a pair, triplet or even a subarray.

Some common problems with the two-pointer method:

Squaring a sorted array
Triplets that sum to zero
Comparing strings that contain backspaces

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

How to identify the fast and slow pointers problem

A

Fast and slow problems will typically deal with a loop in a linked list or array. You can also use the pattern when you need to know the position of a certain element or the overall length of the linked list.

Some common problems with the fast and slow pointers pattern:

Linked list cycle
Palindrome linked list
Cycle in a circular array

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

How to identify the merge-intervals pattern

A

You have a merge-intervals pattern if you’re asked to produce a list with only mutually exclusive intervals or you hear the term “overlapping intervals.”

Some common problems with the merge-intervals pattern:

Intervals intersection
Maximum CPU load

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

What is a merge interval pattern?

A

The merge intervals pattern is an efficient technique to work through overlapping intervals. In many problems involving intervals, you either need to find overlapping intervals or merge intervals if they overlap.

The merge-interval pattern works like this: Given two intervals “a” and “b,” there will be six different ways the two intervals can relate to each other

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

How to identify the cyclic-sort pattern

A

You can use the cyclic-sort pattern on problems involving a sorted array with numbers in a given range or if the problem asks you to find the missing, duplicate or smallest number in a sorted or rotated array.

Some common problems with a cyclic-sort pattern:

Find the missing number
Find the smallest missing positive number

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

How to identify the in-place reversal of linked list pattern

A

You will be asked to reverse a linked list without using extra memory.

Some common problems featuring the in-place reversal of linked list pattern:

Reverse a sublist
Reverse every K-element sublist

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

How to identify the tree breadth-first pattern

A

If you’re asked to traverse a tree in a level-by-level fashion or level-order traversal, you can use the tree breadth-first pattern.

Some common problems featuring the tree breadth-first pattern:

Binary tree level order traversal
Zig-zag traversal

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

How to identify the tree depth-first pattern

A

If you’re asked to traverse a tree with in-order, preorder, or postorder depth-first search or the problem requires searching for something where the node is closer to a leaf, you can use the tree depth-first pattern.

Some common problems featuring the tree depth-first search pattern:

Sum of path numbers
All paths for a sum

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

How to identify the two heaps pattern?

A

The two heaps pattern is useful in priority queue and scheduling situations. If you need to find the smallest, largest or median elements of a set or solve a problem with a binary tree data structure, you can use the two heaps pattern.

Some common problems featuring the two heaps pattern:

Find the median of a number stream

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

How to identify the subsets pattern?

A

You can use the subsets pattern when you need to find the combinations or permutations of a given set.

Some common problems featuring the subsets pattern:

Subsets with duplicates
String permutations by changing case

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

How to identify the modified binary search pattern?

A

You can use the modified binary search pattern to solve order-agnostic binary search or search in a sorted infinite array problems.

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

How to identify the top “K” elements pattern?

A

If you’re asked to find the top, smallest or frequent “K” elements of a given set or sort an array to find an exact element, you can use the top “K” elements pattern.

Some common problems featuring the top “K” elements pattern:

Top “K” numbers
Top “K” frequent numbers

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

How to identify the K-way merge pattern

A

The K-way merge pattern can be used in problems featuring sorted arrays, lists or a matrix. You can also use the pattern if you need to merge sorted lists or find the smallest element in a sorted list.

Some common problems featuring the K-way merge pattern:

Merge K-sorted lists
“K” pairs with largest sums

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

How to identify the topological sort pattern?

A

If you’re asked to update all objects in a sorted order or have a class of objects in a particular order, you can use the topological sort pattern. The problem might also deal with graphs that have no directed cycles in some problems.

Some common problems featuring the topological sort pattern:

Task scheduling
Minimum height of a tree

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

When do you use prefix sum?

A

Any sequential problems that require summation.

17
Q

When do you use sentinel node?

A

When you have to remove the first node in a linked list

18
Q

When to identify backtracking problems?

A

There are three types of problems in backtracking –

Decision Problem – In this, we search for a feasible solution.
Optimization Problem – In this, we search for the best solution.
Enumeration Problem – In this, we find all feasible solutions.

example, consider the SudoKo solving Problem, we try filling digits one by one. Whenever we find that current digit cannot lead to a solution, we remove it (backtrack) and try next digit

“choose, explore, unchoose”

19
Q

What are 3 components of a dynamic programming algorithm?

A

First, we need some function or array that represents the answer to the problem for a given state.

Second, we need a way to transition between states. This is called a recurrence relation and figuring it out is usually the hardest part of solving a problem with dynamic programming

Third component is establishing base cases.

20
Q

How do you identify dynamic programming problems?

A
  • When recursions use the repeated parameters/arguments
  • Question is asking for the “number of ways” to do something
  • We need to make decisions that may depend on previously made decisions
21
Q

How do you identify problems that requires keeping track of the “balance” or “count”?

A

Problems that has implicit implications for a stack

22
Q

What are the steps of solving dynamic programming?

A
  1. Start with the recursive backtracking solution
  2. Optimize by using a memoization table (top-down dynamic programming)
  3. Remove the need for recursion (bottom-up dynamic programming)
  4. Apply final tricks to reduce the time / memory complexity