Skip to content
Coding Preparation

icon picker
Data Structures & Algos

ARRAYS

What:

An array is one of the most simple data structures in programming and can simply be thought of as a collection of items. These items can be any type, however, all items in the array must be of the same type. An easy way to visualize an array is like this:
{{Make a picture with boxes with numbers in them as well as their indexes below. Also have array.length = # of items}}
Arrays are used for convenient storing and editing multiple pieces of data.

Common Approaches to Problems:

Two Pointer
One of the most common strategies to use when faced with an array problem is the two pointer approach. Essentially, we utilize two pointers to iterate through the array concurrently until 1 or both of them reaches a concluding condition. The pointers, which we can refer to as i and j, keep track of indices in the array. There are generally w
This is one of the most useful and efficient techniques to employ during a coding interview, specifically because it optimizes common solutions that have a time complexity O(n^2).
Sliding Window
The Sliding Window pattern is used to perform a required operation on a specific window size of a given array or linked list, such as finding the longest subarray containing all 1s. 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.
HashSets
When checking uniqueness or repetition
HashMaps
For frequencies
Important Method to Remember : map.getOrDefault(n, 0)
Iterating Over Hashmap:
image.jpeg
Greedy Approach
Have a current maximum or minimum, and a global maximum or minimum
Binary Search
Start from the middle

Common Tricks to Use:

Arrays.SORT

STRINGS


Common Approaches to Problems:

Two Pointer
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. Two Pointers is often useful when searching pairs in a sorted array or linked list; for example, when you have to compare each element of an array to its other elements.
Convert to Array
Convert to an array and use the tricks listed under the Arrays Section

Common Tricks to Use:

Subtract Character from ‘0’
StringBuilders
Trim
Split
Reverse
To Char Array
Misc: Number of Distinct Substrings in a String is n(n+1)/2

LINKED LISTS


Common Approaches to Problems:

Simple Pointer Manipulation
Such as Swapping
Two Pointers
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. Two Pointers is often useful when searching pairs in a sorted array or linked list; for example, when you have to compare each element of an array to its other elements.
Addition: Set to check if Node was visited
Slow and Fast Pointers
The Fast and Slow pointer approach, also known as the Hare & Tortoise algorithm, is a pointer algorithm that uses two pointers which move through the array (or sequence/linked list) at different speeds. This approach is quite useful when dealing with cyclic linked lists or arrays.
Helps you reach to middle to a Singly Linked List (Slow at Mid, Fast at End)
Two pointers are bound to meet: The fast pointer should catch the slow pointer once both the pointers are in a cyclic loop.

In-Place Reversal

Common Tricks to Use:

DummyNode as Head: Node DummyHead = new Node (0), DummyHead.next = head
HashMap for Cloning: HashMap that maps Actual node to Cloned Node
HashSet for Duplicates



BINARY TREES


Common Approaches to Problems:

Depth First Search: Stack or Recursion (PreOrder, InOrder, PostOrder)
When they ask you to go preorder, post order, in order
When they require searching for something where node is closer to a leaf
Breadth FIrst Search: Queue (LevelOrder)
When they ask you to go level by level
Recursion

Common Tricks to Use:



GRAPHS

Common Approaches to Problems:

Depth First Search: Stack
Breadth FIrst Search: Queue
Dijkstra's Algorithm
Topological Sort
Recursion

Common Tricks to Use:

HashMap for Cloning: HashMap that maps Actual node to Cloned Node

MATRICES


Common Approaches to Problems:

Binary Search
Treat the 2D Array as a 1D Array and perform Binary Search
Row: i / totalCols and Col: i % totalCols where i = 1DimensionalIndex
Flood Fill Algorithm
if (r >= 1) : dfs(up row, same column)
if (r+1 < image.length) : dfs(down row, same column)
if (c >= 1) : dfs(same row, column)
if (c+1 < image[0].length): dfs(same row, down column)

OBJECT ORIENTED DESIGN

Ask: Who, What, Where, When, Why, How
Define the Core Objects
Analyze Relationships between these Objects
Investigate ACTIONS

HEAPS

When to Use: Top K, Least K,
Implement Using: Priority Queue<Integer> pq = new PriorityQueue<>();

STACK

When to Use: Reversal, DFS
Implement Using: Stack<Integer> s = new Stack<Integer>();

QUEUE

When to Use: Reversal, BFS
Implement Using: Queue<Integer> q = new LinkedList<>();

BACKTRACKING: PERMUTATIONS & SUBSETS

Pseudocode Template
image.jpeg
Generally, add candidate to the set list, backtrack, and then remove after.
Applications
Subsets. Permutations, Substrings, Combination Sum, Path Sum in Binary Tree

SORTING ALGORITHMS

Bubble Sort
Selection Sort
Insertion Sort
Merge Sort
Quick Sort




Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.