0PricingLogin
DSA Interview Prep · Lesson

Pattern Recognition Cheat Sheet

Map 15 common problem signals (sorted array, need all combos, maximise value with constraint, etc.) to the algorithm patterns that solve them fastest.

The 60-Second Pattern Recognition Game

In a real interview, you have roughly 60 seconds after reading a problem to identify which algorithmic pattern applies before the interviewer expects you to start coding. This is the most important skill to develop — not memorising implementations, but recognising which tool to reach for.

Pattern recognition comes from mapping problem signals (words and constraints in the problem statement) to known algorithm families. Once you identify the pattern, the implementation becomes a template-filling exercise. This lesson is a systematic cheat sheet of the 15 most common problem signals and their corresponding patterns.

# The recognition process
recognition_steps = [
    '1. Read the problem once fully (do not start coding)',
    '2. Identify the data structure: array, string, tree, graph, matrix?',
    '3. Identify the ask: find min/max, count ways, enumerate, detect cycle...?',
    '4. Note the constraint: n<=20 (bitmask), sorted (binary search), DAG (topo sort)?',
    '5. Map signal -> pattern',
    '6. State the pattern and complexity to the interviewer before coding',
    '7. Handle edge cases mentally before writing',
]
for step in recognition_steps:
    print(step)

Signal 1-3: Array and String Patterns

The most frequent problem signals for arrays and strings:

  • Sorted array + find target → Binary search O(log n)
  • Find pair/triplet summing to target → Two pointers O(n) if sorted, hash map O(n) if unsorted
  • Longest/shortest subarray/substring satisfying condition → Sliding window O(n)
  • Contiguous subarray max/min sum → Kadane's algorithm O(n)
  • Duplicate detection → Hash set O(n) or sort O(n log n)

If the array is sorted, always consider binary search first. Unsorted + target sum + O(n) = almost always a hash map for the complement lookup.

# Quick recognition: array/string signals
signals = [
    ('Sorted array, find element',           'Binary search O(log n)'),
    ('Find two elements summing to K',        'Sort+two-ptr O(n log n) or hash O(n)'),
    ('Longest subarray with property P',      'Sliding window (variable size) O(n)'),
    ('Max sum contiguous subarray',           'Kadane algorithm O(n)'),
    ('Anagram/permutation check',             'Frequency map (Counter) O(n)'),
    ('Contains duplicate',                    'Hash set O(n)'),
    ('Merge two sorted arrays/lists',         'Two pointers O(n+m)'),
    ('Rotate / shift array',                  'Reverse trick O(n) in-place'),
    ('Next permutation',                      'Find rightmost ascent + swap + reverse'),
    ('Maximum product subarray',              'Track max and min (handles negatives)'),
]
for signal, pattern in signals:
    print(f'{signal:45s} => {pattern}')

All lessons in this course

  1. Pattern Recognition Cheat Sheet
  2. Timed Mock Interview: Easy and Medium Problems
  3. Handling Edge Cases and Interviewee Communication
  4. Hard Problem Walkthroughs: Word Ladder II and Alien Dictionary
← Back to DSA Interview Prep