Quick Reference Guide

Pattern Recognition Cheat Sheet

When you see these keywords → Think these patterns

Problem KeywordsPatternExample Problems
”sorted array”, “find pair/triplet”Two PointersTwo Sum II, 3Sum, Container With Water
”contiguous subarray”, “substring”Sliding WindowMax Sum Subarray, Longest Substring
”sorted”, “find in log(n)“Binary SearchSearch Insert Position, Find Peak
”find in O(1)”, “frequency”, “pairs”HashMapTwo Sum, Anagrams, Subarray Sum
”k most/least”, “top k”HeapTop K Elements, Kth Largest
”graph/tree traversal”DFS/BFSIsland Count, Level Order
”overlapping problems”, “optimal choice”DPClimbing Stairs, Coin Change
”connected components”, “cycles”Union FindNumber of Islands, Redundant Connection

Complexity Quick Reference

PatternTypical TimeTypical Space
Two PointersO(n)O(1)
Sliding WindowO(n)O(k)
Binary SearchO(log n)O(1)
HashMapO(n)O(n)
HeapO(n log k)O(k)
DFS/BFSO(V + E)O(V)
DPO(n²) to O(n)O(n)

Java Version Quick Notes

Java 17 Features (Available)

  • Sealed classes
  • Pattern matching for instanceof
  • Text blocks
  • Records
  • Switch expressions (enhanced)

Java 21+ Features (Mark with comment)

// Java 21+ - Pattern matching for switch
String result = switch (obj) {
    case Integer i when i > 0 -> "positive";
    case Integer i -> "non-positive";
    case String s -> s;
    default -> "unknown";
};
 
// Java 21+ - Record patterns
record Point(int x, int y) {}
if (obj instanceof Point(int x, int y)) {
    // Use x and y directly
}
 
// Java 21+ - Unnamed patterns
if (obj instanceof Point(int x, _)) {
    // Don't care about y
}
 
// Java 21+ - Virtual threads
Thread.startVirtualThread(() -> {
    // Lightweight thread for I/O-bound tasks
});

Common Pitfalls & Tips

Two Pointers

  • ✅ Remember to sort if needed (check if sorting changes problem)
  • ❌ Don’t forget to skip duplicates in 3Sum-style problems
  • ✅ Choose right pointer direction: opposite or same direction

Sliding Window

  • ✅ Expand until invalid, then shrink
  • ❌ Don’t recalculate window from scratch each time
  • ✅ Use array for small alphabets, HashMap for large
  • ✅ Use left + (right - left) / 2 to avoid overflow
  • ❌ Watch infinite loops in right boundary search (need +1)
  • ✅ Verify monotonicity for “binary search on answer”

HashMap

  • ✅ Use getOrDefault() for cleaner code
  • ❌ Don’t modify keys after insertion
  • ✅ Consider LinkedHashMap if order matters

General

  • ✅ Always analyze space-time tradeoffs
  • ❌ Don’t optimize prematurely (correct first, fast second)
  • ✅ Test edge cases: empty, single element, duplicates

Interview Strategy

  1. Clarify (2 min)

    • Input constraints (size, range, duplicates?)
    • Output format
    • Edge cases
  2. Plan (5 min)

    • Identify pattern
    • Discuss brute force
    • Optimize (what’s bottleneck?)
  3. Implement (20 min)

    • Start with helper functions
    • Handle edge cases
    • Use meaningful names
  4. Test (5 min)

    • Walk through example
    • Edge cases
    • Complexity analysis
  5. Optimize (3 min)

    • Discuss alternatives
    • Space-time tradeoffs

Compile & Run

# Compile single file
javac src/patterns/TwoPointers.java
 
# Run
java -cp src patterns.TwoPointers
 
# Compile all
javac src/patterns/*.java
 
# With Java 21 features (if using Java 21+)
javac --release 21 src/patterns/*.java
java -cp src patterns.PatternName

Next Steps

  1. ✅ Review pattern in src/patterns/
  2. ✅ Read practice session in practice-sessions/
  3. ✅ Attempt problem without looking at solution
  4. ✅ Compare your solution with provided approaches
  5. ✅ Understand tradeoffs
  6. ✅ Practice explaining complexity analysis