🎓 A1Learner
🔍

DSA From Answers to Patterns

Pattern recognition is the skill of spotting which known method fits a new problem. This course trains that skill by hiding the pattern inside each problem statement.

The brute force checks every pair

A brute force solution tries possibilities in the plainest way. Its cost is easy to count.

def has_pair_brute(nums, target):
    checks = 0
    for i in range(len(nums)):
        for j in range(i + 1, len(nums)):
            checks += 1
            if nums[i] + nums[j] == target:
                return True, checks
    return False, checks

print(has_pair_brute([2, 7, 11, 15], 26))

A set answers in one pass

A set stores each value seen so far. Each new value checks for its partner in one step.

def has_pair_fast(nums, target):
    seen = set()
    checks = 0
    for value in nums:
        checks += 1
        if target - value in seen:
            return True, checks
        seen.add(value)
    return False, checks

print(has_pair_fast([2, 7, 11, 15], 26))

Clarifying fixes the rules first

A clarifying question pins down one rule before any code is written. The answer changes what correct means.

words = ["Tea", "tea", "TEA"]
print(len(set(words)))
print(len(set(w.lower() for w in words)))

Example

def brute(nums, target):
    checks = 0
    for i in range(len(nums)):
        for j in range(i + 1, len(nums)):
            checks += 1
            if nums[i] + nums[j] == target:
                return (nums[i], nums[j]), checks
    return None, checks

def two_pointer(nums, target):
    ordered = sorted(nums)
    left, right = 0, len(ordered) - 1
    checks = 0
    while left < right:
        checks += 1
        total = ordered[left] + ordered[right]
        if total == target:
            return (ordered[left], ordered[right]), checks
        if total < target:
            left += 1
        else:
            right -= 1
    return None, checks

def hashed(nums, target):
    seen = set()
    checks = 0
    for value in nums:
        checks += 1
        if target - value in seen:
            return (target - value, value), checks
        seen.add(value)
    return None, checks

nums = [8, 3, 5, 1, 9, 4, 7, 2]
print("brute:", brute(nums, 12))
print("two pointers:", two_pointer(nums, 12))
print("hash set:", hashed(nums, 12))

Key points

  • Pattern recognition: naming the known method that fits a problem
  • Brute force: the plainest correct solution, written first
  • The interview loop runs clarify, brute force, optimise, code, then test
  • Operation counts compare two solutions without depending on the machine
  • A recognised pattern turns a new problem into a known one

In this example, one problem asks for two values that sum to 12. The brute force version checks pairs in order and finds 8 and 4 after five checks. The two-pointer version sorts first and closes in from the ends in three checks. The set version walks once and meets the partner of 9 after five checks. Each version returns a correct pair, and the counts show what the pattern bought on this input.

Note

Python hides some loops inside built-in tools, so this course counts steps by hand

Try it Yourself

Edit the code, then press Run.

Running is simulated · Python Playground

Exercise

A problem statement never names its method. What is the first solution to write?