Skip to lesson

DSA revision sheet

A consolidated revision sheet of high-yield templates, asymptotic complexity reference tables, and common implementation traps across core data structures.

Algorithmic pattern cheatsheet and boilerplate templates

Before entering technical coding rounds, you should be able to write canonical algorithmic boilerplate flawlessly from muscle memory. Having standard templates memorized prevents silly syntax errors and frees your mental bandwidth to focus on core problem logic.

Example 1 · Canonical Binary Search Template

BinarySearchTemplateDemo.javaJava
public class BinarySearchTemplateDemo {    public static int binarySearch(int[] arr, int target) {        int low = 0, high = arr.length - 1;        while (low <= high) {            int mid = low + (high - low) / 2;            if (arr[mid] == target) return mid;            else if (arr[mid] < target) low = mid + 1;            else high = mid - 1;        }        return -1; // Not found    }
    public static void main(String[] args) {        int[] sorted = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91};        System.out.println("Index of 23: " + binarySearch(sorted, 23));        System.out.println("Index of 50: " + binarySearch(sorted, 50));    }}
Output
Index of 23: 5
Index of 50: -1

What this code does

  • Finds 23 at index 5 in O(log N) time.
  • Returns -1 when 50 is absent without throwing exceptions.
  • Guards against 32-bit overflow using low + (high - low) / 2.

Example 2 · Universal Sliding Window Template

SlidingWindowTemplateDemo.javaJava
import java.util.*;
public class SlidingWindowTemplateDemo {    public static int lengthOfLongestSubstring(String s) {        Map<Character, Integer> lastSeen = new HashMap<>();        int maxLen = 0, left = 0;
        for (int right = 0; right < s.length(); right++) {            char c = s.charAt(right);            if (lastSeen.containsKey(c)) {                left = Math.max(left, lastSeen.get(c) + 1);            }            lastSeen.put(c, right);            maxLen = Math.max(maxLen, right - left + 1);        }        return maxLen;    }
    public static void main(String[] args) {        System.out.println("Longest in abcabcbb: " + lengthOfLongestSubstring("abcabcbb"));        System.out.println("Longest in bbbbb: " + lengthOfLongestSubstring("bbbbb"));    }}
Output
Longest in abcabcbb: 3
Longest in bbbbb: 1

What this code does

  • For abcabcbb, longest substring is abc with length 3.
  • For bbbbb, longest substring is b with length 1.
  • Left pointer leaps forward in O(1) time without redundant character steps.

Example 3 · Level-Order BFS Queue Template

BfsTemplateDemo.javaJava
import java.util.*;
public class BfsTemplateDemo {    static class TreeNode {        int val;        TreeNode left, right;        TreeNode(int val) { this.val = val; }    }
    public static List<List<Integer>> levelOrder(TreeNode root) {        List<List<Integer>> res = new ArrayList<>();        if (root == null) return res;
        Queue<TreeNode> queue = new ArrayDeque<>();        queue.offer(root);
        while (!queue.isEmpty()) {            int levelSize = queue.size();            List<Integer> level = new ArrayList<>();            for (int i = 0; i < levelSize; i++) {                TreeNode cur = queue.poll();                level.add(cur.val);                if (cur.left != null) queue.offer(cur.left);                if (cur.right != null) queue.offer(cur.right);            }            res.add(level);        }        return res;    }
    public static void main(String[] args) {        TreeNode root = new TreeNode(1);        root.left = new TreeNode(2);        root.right = new TreeNode(3);        System.out.println("Levels: " + levelOrder(root));    }}
Output
Levels: [[1], [2, 3]]

What this code does

  • Root 1 forms level 0.
  • Children 2 and 3 form level 1.
  • queue.size() snapshots ensure clean level-by-level separation.

Binary Search Template: always use low = 0, high = n - 1, and while (low <= high). Compute mid with low + (high - low) / 2 to prevent 32-bit signed integer overflow. Update low = mid + 1 or high = mid - 1. Sliding Window Template: expand right pointer across the array in an outer loop. While the window condition is violated (e.g. duplicate character or sum > target), shrink left pointer from the window. Update your optimal answer at each valid window state in strictly O(N) time.

Comprehensive time and space complexity matrix

Interviewers expect instantaneous recall of time and space complexities across standard operations.

Arrays and Strings: Index access O(1), Search O(N), Insert/Delete at end O(1) amortized, Insert/Delete at arbitrary index O(N). Linked Lists: Search O(N), Insert/Delete given node pointer O(1), Prepend O(1), Append O(1) with tail pointer. Binary Search Trees (Balanced AVL/Red-Black): Search O(log N), Insert O(log N), Delete O(log N). Worst case skewed tree degrades to O(N). Hash Maps: Search O(1) average, Insert O(1) average, Delete O(1) average. Degrades to O(N) on worst-case collisions (or O(log N) in Java 8+). Heaps (PriorityQueue): Peek O(1), Offer O(log N), Poll O(log N), Build Heap O(N).

The five most common coding round bugs

Five high-frequency bugs separate candidates who finish coding rounds from those who do not. Compare each safe line below with the risky habit it replaces (shown after the code).

Java
int mid = left + (right - left) / 2;  // 1. not (left + right) / 2visited[next] = true;                 // 3. mark visited when you enqueuequeue.offer(next);ListNode temp = curr.next;            // 4. save next before overwriting itcurr.next = prev;for (int i = 1; i <= n; i += i & (-i)) {    tree[i] += delta;                 // 5. Fenwick loops start at 1, never 0}
  • Integer overflow: summing large integers or computing (left + right) / 2 without using long or the subtraction formula.
  • Binary search off-by-one: using < instead of <=, or forgetting to adjust boundaries with mid +/- 1.
  • Graph infinite loops: failing to mark a node as visited before adding it to a BFS queue.
  • Linked list pointer loss: overwriting curr.next before storing temp = curr.next.
  • Fenwick tree zero index: attempting to update or query index 0, causing an infinite loop (i += 0).

Last-minute interview prep strategy

During the final 48 hours before an interview, your focus should shift from learning new exotic algorithms to reinforcing core templates and communication speed.

Manage your 45-minute coding interview clock strategically:

  • Minutes 0 to 5: Clarify the problem statement, confirm edge cases (empty inputs, duplicates, scale of N), and walk through two simple examples.
  • Minutes 5 to 10: Outline the brute force, explain the optimal approach, and confirm buy-in from the interviewer before writing code.
  • Minutes 10 to 30: Implement clean, modular code with descriptive variable names.
  • Minutes 30 to 40: Dry run your code manually line-by-line using a small example, catching any boundary bugs.
  • Minutes 40 to 45: State time and space complexity explicitly and answer follow-up questions.

Next step

Check your understanding

10 quick questions