Cracking LeetCode in Java: Key Problems to Master

2025-08-15

If you want to crack coding interviews, you don’t need to solve every LeetCode problem. What you need is mastery of the key patterns that cover 80% of the questions. In Java, you should be fluent in these patterns and able to explain your approach out loud. Below is a roadmap of the essential problems, with short code sketches to practice.


1) Arrays and Two Pointers

Classic: Two Sum II (sorted array).

public int[] twoSum(int[] numbers, int target) {
    int i = 0, j = numbers.length - 1;
    while (i < j) {
        int sum = numbers[i] + numbers[j];
        if (sum == target) return new int[]{i + 1, j + 1};
        if (sum < target) i++;
        else j--;
    }
    return new int[]{-1, -1};
}

Other must-do: Trapping Rain Water, Container With Most Water, Move Zeroes.


2) Sliding Window

Classic: Longest Substring Without Repeating Characters.

public int lengthOfLongestSubstring(String s) {
    Map<Character, Integer> map = new HashMap<>();
    int left = 0, best = 0;
    for (int right = 0; right < s.length(); right++) {
        char c = s.charAt(right);
        if (map.containsKey(c) && map.get(c) >= left) left = map.get(c) + 1;
        map.put(c, right);
        best = Math.max(best, right - left + 1);
    }
    return best;
}

Other must-do: Minimum Window Substring, Permutation in String, Subarray Sum Equals K.


3) Binary Search

Classic: Search in Rotated Sorted Array.

public int search(int[] nums, int target) {
    int lo = 0, hi = nums.length - 1;
    while (lo <= hi) {
        int mid = lo + (hi - lo) / 2;
        if (nums[mid] == target) return mid;
        if (nums[lo] <= nums[mid]) {
            if (nums[lo] <= target && target < nums[mid]) hi = mid - 1;
            else lo = mid + 1;
        } else {
            if (nums[mid] < target && target <= nums[hi]) lo = mid + 1;
            else hi = mid - 1;
        }
    }
    return -1;
}

Other must-do: First Bad Version, Find Peak Element, Koko Eating Bananas (binary search on answer).


4) Linked List

Classic: Reverse a Linked List.

public ListNode reverseList(ListNode head) {
    ListNode prev = null, cur = head;
    while (cur != null) {
        ListNode nxt = cur.next;
        cur.next = prev;
        prev = cur;
        cur = nxt;
    }
    return prev;
}

Other must-do: Merge Two Sorted Lists, Detect Cycle, Reorder List, Add Two Numbers.


5) Dynamic Programming

Classic: Climbing Stairs (Fibonacci DP).

public int climbStairs(int n) {
    if (n <= 2) return n;
    int a = 1, b = 2;
    for (int i = 3; i <= n; i++) {
        int c = a + b;
        a = b;
        b = c;
    }
    return b;
}

Other must-do: House Robber I/II, Coin Change, Longest Increasing Subsequence, Edit Distance, Unique Paths.


6) Greedy

Classic: Jump Game.

public boolean canJump(int[] nums) {
    int far = 0;
    for (int i = 0; i < nums.length; i++) {
        if (i > far) return false;
        far = Math.max(far, i + nums[i]);
    }
    return true;
}

Other must-do: Jump Game II, Gas Station, Partition Labels, Merge Intervals.


7) Graphs and BFS/DFS

Classic: Number of Islands.

public int numIslands(char[][] grid) {
    int m = grid.length, n = grid[0].length, count = 0;
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            if (grid[i][j] == '1') {
                dfs(grid, i, j);
                count++;
            }
        }
    }
    return count;
}
void dfs(char[][] g, int i, int j) {
    if (i < 0 || j < 0 || i >= g.length || j >= g[0].length || g[i][j] == '0') return;
    g[i][j] = '0';
    dfs(g, i + 1, j); dfs(g, i - 1, j); dfs(g, i, j + 1); dfs(g, i, j - 1);
}

Other must-do: Clone Graph, Course Schedule (topological sort), Word Ladder, Pacific Atlantic Water Flow.


8) Heap / Priority Queue

Classic: Kth Largest Element in an Array.

import java.util.*;
public int findKthLargest(int[] nums, int k) {
    PriorityQueue<Integer> pq = new PriorityQueue<>();
    for (int n : nums) {
        pq.add(n);
        if (pq.size() > k) pq.poll();
    }
    return pq.peek();
}

Other must-do: Merge K Sorted Lists, Top K Frequent Elements, Meeting Rooms II.


9) Backtracking

Classic: Generate Parentheses.

public List<String> generateParenthesis(int n) {
    List<String> res = new ArrayList<>();
    backtrack(res, new StringBuilder(), 0, 0, n);
    return res;
}
void backtrack(List<String> res, StringBuilder sb, int open, int close, int n) {
    if (sb.length() == 2 * n) {
        res.add(sb.toString());
        return;
    }
    if (open < n) {
        sb.append('(');
        backtrack(res, sb, open + 1, close, n);
        sb.deleteCharAt(sb.length() - 1);
    }
    if (close < open) {
        sb.append(')');
        backtrack(res, sb, open, close + 1, n);
        sb.deleteCharAt(sb.length() - 1);
    }
}

Other must-do: Subsets, Permutations, Combination Sum, Word Search, N-Queens.


10) Advanced / Mix of Patterns

  • LRU Cache (HashMap + Doubly Linked List)
  • Median of Two Sorted Arrays (binary search partitioning)
  • Trapping Rain Water (two pointers or stack)
  • Edit Distance (2D DP)
  • Wildcard Matching / Regular Expression Matching (DP + string)

Interview Checklist

  • Restate problem clearly before coding.
  • Identify the category (array, dp, graph, etc).
  • Recall the core pattern and adapt.
  • State time and space complexity.
  • Walk a small example step by step.

Final Note

If you master one solid example for each of these categories and know how to explain the approach, you’ll be prepared for most Java coding interviews.

And if you want instant outlines of solutions while practicing, so you can focus on reasoning and explanations rather than being stuck, you can use StealthCoder to capture prompts and see clean Java answers mid-practice.