Skip to content
advancedPhase 8 · Interview Prep

Mixed Pattern Problems

Solve problems combining multiple patterns to build fluency.

2h
10 problems
Topic Progress0%

Pattern Recognition Strategy

The 5-Step Problem Solving Framework

  1. Read & Understand (2 min): Read the problem twice. Identify input/output, constraints, and edge cases.
  2. Identify Pattern (3 min): Look for keywords that signal specific patterns.
  3. Plan Approach (3 min): Outline your solution before coding.
  4. Code (15-20 min): Write clean, modular code.
  5. Test & Optimize (5 min): Walk through examples and edge cases.

Pattern Signal Words

Pattern Signal Words
Two Pointers sorted, pair, palindrome, swap
Sliding Window subarray, substring, consecutive, contiguous
Binary Search sorted, find minimum/maximum, k-th element
DFS/BFS tree, graph, connected, path, level
Dynamic Programming count ways, minimum/maximum, can you, overlapping subproblems
HashMap frequency, duplicate, two sum, anagram
Stack parentheses, next greater, histogram, monotonic
Greedy interval, activity, task, schedule
Union-Find connected components, merge, group
Topological Sort dependency, prerequisite, task ordering

Java Template for Quick Pattern Selection

// Common imports you'll need
import java.util.*;
import java.util.stream.*;

// When you see 'sorted array' + 'pair' -> Two Pointers
int left = 0, right = arr.length - 1;
while (left < right) {
    int sum = arr[left] + arr[right];
    if (sum == target) return new int[]{left, right};
    else if (sum < target) left++;
    else right--;
}

// When you see 'subarray' + 'sum/length' -> Sliding Window
int windowSum = 0, maxSum = 0;
for (int i = 0; i < k; i++) windowSum += arr[i];
maxSum = windowSum;
for (int i = k; i < arr.length; i++) {
    windowSum += arr[i] - arr[i - k];
    maxSum = Math.max(maxSum, windowSum);
}

// When you see 'tree' + 'path/depth' -> DFS
int depth(TreeNode node) {
    if (node == null) return 0;
    return 1 + Math.max(depth(node.left), depth(node.right));
}

Combining Multiple Patterns

Pattern 1: DFS + Backtracking

// Example: Generate all permutations
List<List<Integer>> permute(int[] nums) {
    List<List<Integer>> result = new ArrayList<>();
    backtrack(result, new ArrayList<>(), nums);
    return result;
}

void backtrack(List<List<Integer>> result, List<Integer> temp, int[] nums) {
    if (temp.size() == nums.length) {
        result.add(new ArrayList<>(temp));
        return;
    }
    for (int i = 0; i < nums.length; i++) {
        if (temp.contains(nums[i])) continue;
        temp.add(nums[i]);
        backtrack(result, temp, nums);
        temp.remove(temp.size() - 1);
    }
}

Pattern 2: HashMap + Sorting

// Example: Group anagrams
Map<String, List<String>> groupAnagrams(String[] strs) {
    Map<String, List<String>> map = new HashMap<>();
    for (String s : strs) {
        char[] chars = s.toCharArray();
        Arrays.sort(chars);
        String key = new String(chars);
        map.computeIfAbsent(key, k -> new ArrayList<>()).add(s);
    }
    return map;
}

Pattern 3: BFS + Graph

// Example: Level order traversal with BFS
List<List<Integer>> levelOrder(TreeNode root) {
    List<List<Integer>> result = new ArrayList<>();
    if (root == null) return result;
    Queue<TreeNode> queue = new LinkedList<>();
    queue.offer(root);
    while (!queue.isEmpty()) {
        int size = queue.size();
        List<Integer> level = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            TreeNode node = queue.poll();
            level.add(node.val);
            if (node.left != null) queue.offer(node.left);
            if (node.right != null) queue.offer(node.right);
        }
        result.add(level);
    }
    return result;
}

Pattern 4: Binary Search + DP

// Example: Longest Increasing Subsequence (patience sorting)
int lengthOfLIS(int[] nums) {
    List<Integer> sub = new ArrayList<>();
    for (int x : nums) {
        if (sub.isEmpty() || sub.get(sub.size() - 1) < x) {
            sub.add(x);
        } else {
            int idx = Collections.binarySearch(sub, x);
            if (idx < 0) idx = -(idx + 1);
            sub.set(idx, x);
        }
    }
    return sub.size();
}

Interview Time Management

Phase Time Budget Activity
Understanding 2-3 min Read problem, clarify constraints
Planning 3-5 min Identify pattern, outline solution
Coding 15-20 min Write clean code with comments
Testing 5 min Walk through examples, edge cases
Optimization 2-3 min Discuss Big-O, suggest improvements

Total: ~30 minutes per problem

Practice Problems

0/1solved
Subarray Sum Equals K
HashMap + Prefix Sum

Given an array of integers nums and an integer k, return the total number of subarrays whose sum equals to k.

Example:

Input: nums = [1,1,1], k = 2

Output: 2

[1,1] appears twice as a contiguous subarray.

Brute Force Solution — O(n²) time, O(1) space

Check all subarrays

class Solution {
    public int subarraySum(int[] nums, int k) {
        int count = 0;
        for (int i = 0; i < nums.length; i++) {
            int sum = 0;
            for (int j = i; j < nums.length; j++) {
                sum += nums[j];
                if (sum == k) count++;
            }
        }
        return count;
    }
}
Optimal Solution — O(n) time, O(n) space

HashMap with prefix sum

class Solution {
    public int subarraySum(int[] nums, int k) {
        Map<Integer, Integer> map = new HashMap<>();
        map.put(0, 1);
        int sum = 0, count = 0;
        for (int num : nums) {
            sum += num;
            if (map.containsKey(sum - k)) {
                count += map.get(sum - k);
            }
            map.put(sum, map.getOrDefault(sum, 0) + 1);
        }
        return count;
    }
}

Edge Cases:

  • All negative numbers
  • k = 0
  • Single element equal to k
  • No subarray sums to k

Quiz

1. You encounter a problem asking to find the longest substring with at most K distinct characters. Which pattern should you use?

Question 1 options

2. When solving a problem involving 'counting all possible ways', which pattern is most likely applicable?

Question 2 options

3. What is the primary purpose of Mixed Patterns?

Question 3 options

4. What is a common mistake when implementing Mixed Patterns?

Question 4 options

Flashcards

Question

What are the signal words for Sliding Window?

Answer

subarray, substring, consecutive, contiguous, longest/shortest with condition

Question

When should you combine HashMap with sorting?

Answer

When you need grouping (anagrams), frequency analysis, or need to find pairs after organizing data

Question

What is Mixed Patterns?

Answer

Mixed Patterns is a key concept in software engineering.

Question

When to use Mixed Patterns?

Answer

Use Mixed Patterns when building production systems that require reliability, scalability, and maintainability.

Question

Mixed Patterns best practices

Answer

Follow SOLID principles, write clean code, test thoroughly, document decisions, and monitor in production.

Revision Notes

Key Takeaways

  • 1.Pattern recognition comes from practice - solve 200+ problems across all patterns
  • 2.Signal words in problem descriptions are your best friend for pattern selection
  • 3.When stuck, start with brute force and optimize from there
  • 4.Time management is critical - practice solving under 30-minute constraints

Interview Tips

  • If you can't identify the pattern in 5 minutes, start with brute force approach
  • Always ask the interviewer about constraints - they may hint at the pattern
  • Practice identifying patterns from problem titles alone before reading full descriptions
  • Keep a personal 'pattern cheat sheet' and review it daily before practice sessions

Cheat Sheet

Mixed Patterns Cheat Sheet

Pattern Selection Quick Guide:

  • Sorted array + pair? -> Two Pointers
  • Subarray/substring + sum/length? -> Sliding Window
  • Tree/graph traversal? -> DFS or BFS
  • Count ways + overlapping subproblems? -> Dynamic Programming
  • Dependency ordering? -> Topological Sort
  • Connected components? -> Union-Find
  • Parentheses/next greater? -> Monotonic Stack

Combination Patterns:

  • DFS + Backtracking (permutations, combinations)
  • HashMap + Sorting (grouping, anagrams)
  • BFS + Graph (level order, shortest path)
  • Binary Search + DP (optimization problems)

Time Management:

  • 2 min: Read & understand
  • 3 min: Identify pattern
  • 3 min: Plan approach
  • 15-20 min: Code
  • 5 min: Test & optimize