Coding Patterns Flashcards

1
Q

Sliding Window

A

Sliding Windows start from the 1st element and keep shifting right by one element and adjust the length of the window according to the problem that you are solving. In some cases, the window size remains constant and in other cases the sizes grows or shrinks.

  • The probleminput is a linear data structure such as a linked list, array, or string
  • You’reasked to find the longest/shortestsubstring, subarray, or a desired value
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Two Pointers

A

Two Pointers is a pattern where two pointers iterate through the data structure in tandem until one or both of the pointers hit a certain condition. Helps reduce one point n² to n

  • It will feature problems where you deal with sorted arrays (or Linked Lists) and need to find a set of elements that fulfill certain constraints
  • The set of elements in the array is a pair, a triplet, or even a subarray
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. Fast and Slow pointers
A

Two pointers that move through the array (or sequence/linked list)at different speeds.This approach is quite useful when dealing with cyclic linked lists or arrays.

  • The problem will deal with a loop in a linked list or array
  • When you need to know the position of a certain element or the overall length of the linked list.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. Merge Intervals
A

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
5
Q
  1. Reverse Linked List
A

This pattern reverses one node at a time starting with one variable (current) pointing to the head of the linked list, and one variable (previous) will point to the previous node that you have processed. In a lock-step manner, you will reverse the current node by pointing it to the previous before moving on to the next node. Also, you will update the variable “previous” to always point to the previous node that you have processed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. Reverse Linked List
A

This pattern reverses one node at a time starting with one variable (current) pointing to the head of the linked list, and one variable (previous) will point to the previous node that you have processed. In a lock-step manner, you will reverse the current node by pointing it to the previous before moving on to the next node. Also, you will update the variable “previous” to always point to the previous node that you have processed.

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

In-Order Traversal

A

In-order traversal means to “visit” (often, print) the left branch, then the current node, and finally, the right branch. When performed on a binary search tree, it visits the nodes in ascending order (hence the name “in-order”).

void inOrderTraversal(TreeNode node) {
if (node!= null) {
inOrderTraversal(node.left);
visit(node);
inOrderTraversal(node.right);
}
}

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

Pre-Order Traversal

A

Pre-order traversal visits the current node before its child nodes (hence the name “pre-order”).

void inOrderTraversal(TreeNode node) {
if (node!= null) {
visit(node);
inOrderTraversal(node.left);
inOrderTraversal(node.right);
}
}

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

Post-Order Traversal

A

Post-order traversal visits the current node after its child nodes (hence the name “post order”).

void inOrderTraversal(TreeNode node) {
if (node!= null) {
inOrderTraversal(node.left);
inOrderTraversal(node.right);
visit(node);
}
}

5
/ \
3 4
/ \
1 2

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