Thursday, April 9, 2026

DSA Coding patterns of FAANG interviews

Coding Interview Prep

7 DSA Coding Patterns That Crack 80% of FAANG Interviews (2026)

Stop memorising individual problems. Learn the 7 core patterns — and you'll be able to solve hundreds of LeetCode problems you've never seen before.

📅 Updated April 2026  |  ⏱ 14 min read  |  🎯 Beginner to Advanced

Most interview candidates grind 200+ LeetCode problems randomly. The candidates who get offers study patterns. When you see a new problem in an interview, you don't need to have solved it before — you just need to recognize which pattern applies. Here are the 7 that matter most.

1 Sliding Window Arrays · Strings

Use a window that expands right and shrinks from the left to process subarrays or substrings without re-scanning. Turns O(n²) brute force into O(n).

✅ Use When
  • Max/min subarray of size k
  • Longest substring with condition
  • Smallest subarray with sum ≥ target
❌ Not For
  • Non-contiguous elements
  • 2D arrays
  • Problems requiring backtracking

Classic Example: Longest substring without repeating characters

def lengthOfLongestSubstring(s):
    char_set = set()
    left = max_len = 0
    for right in range(len(s)):
        while s[right] in char_set:
            char_set.remove(s[left])
            left += 1
        char_set.add(s[right])
        max_len = max(max_len, right - left + 1)
    return max_len
Time:O(n)
Space:O(k) where k = charset size
🎯 Interview Signal If the problem mentions "subarray", "substring", or "contiguous" + asks for max/min/longest — default to Sliding Window.
2 Two Pointers Sorted Arrays · Linked Lists

Use two indices (usually one from each end, or both starting left) that move toward each other or in the same direction. Works brilliantly on sorted arrays.

✅ Use When
  • Pair with target sum
  • Palindrome check
  • Remove duplicates in-place
❌ Not For
  • Unsorted arrays (usually)
  • When order must be preserved

Classic Example: Two Sum II (sorted array)

def twoSum(numbers, target):
    left, right = 0, len(numbers) - 1
    while left < right:
        s = numbers[left] + numbers[right]
        if s == target:   return [left+1, right+1]
        elif s < target:  left += 1
        else:             right -= 1
Time:O(n)
Space:O(1)
3 Fast & Slow Pointers Linked Lists · Cycles

Also called Floyd's Cycle Detection. One pointer moves 1 step, another moves 2 steps. If there's a cycle, they'll eventually meet. Also finds the middle of a list.

Classic Example: Detect cycle in linked list

def hasCycle(head):
    slow = fast = head
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next
        if slow == fast:
            return True
    return False

# Find middle of linked list:
def middleNode(head):
    slow = fast = head
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next
    return slow  # slow is at the middle
Time:O(n)
Space:O(1) — no extra memory!
🎯 Interview Signal Any problem about cycles, middle node, or detecting loops in a linked list → Fast & Slow Pointers.
4 BFS & DFS Trees · Graphs

BFS explores level by level using a queue. DFS goes deep first using recursion (or a stack). Together they solve nearly all tree and graph problems.

✅ BFS — Use For
  • Shortest path (unweighted)
  • Level-order traversal
  • Nearest neighbors
✅ DFS — Use For
  • Path existence problems
  • Connected components
  • Backtracking (permutations)
# BFS — Level order traversal
from collections import deque
def levelOrder(root):
    if not root: return []
    result, queue = [], deque([root])
    while queue:
        level = []
        for _ in range(len(queue)):
            node = queue.popleft()
            level.append(node.val)
            if node.left:  queue.append(node.left)
            if node.right: queue.append(node.right)
        result.append(level)
    return result
Time:O(n)
Space:O(w) BFS, O(h) DFS
5 Dynamic Programming Optimization · Counting

Break a problem into overlapping subproblems and store results to avoid recomputation. Two styles: Top-down (memoization) and Bottom-up (tabulation).

⚠️ DP Recognition Checklist Ask these 2 questions: (1) Can the problem be broken into smaller subproblems? (2) Do subproblems repeat? If YES to both → try DP.

Classic Example: Fibonacci with memoization vs tabulation

# Memoization (Top-Down)
def fib_memo(n, memo={}):
    if n <= 1: return n
    if n not in memo:
        memo[n] = fib_memo(n-1, memo) + fib_memo(n-2, memo)
    return memo[n]

# Tabulation (Bottom-Up) — O(1) space optimized
def fib_tab(n):
    if n <= 1: return n
    a, b = 0, 1
    for _ in range(2, n+1):
        a, b = b, a + b
    return b
Naive Recursion:O(2ⁿ)
With DP:O(n)
6 Binary Search on Answer Search · Monotonic Functions

Don't just use Binary Search on sorted arrays. The advanced pattern is Binary Search on the answer space — when the answer has a monotonic property (feasibility increases/decreases with the value).

Classic Example: Find minimum capacity to ship packages within D days

def shipWithinDays(weights, days):
    left, right = max(weights), sum(weights)
    def canShip(capacity):
        days_needed, current = 1, 0
        for w in weights:
            if current + w > capacity:
                days_needed += 1
                current = 0
            current += w
        return days_needed <= days
    while left < right:
        mid = (left + right) // 2
        if canShip(mid): right = mid
        else:            left = mid + 1
    return left
🎯 Interview Signal "Minimize the maximum..." or "What is the smallest X such that..." → Binary Search on Answer.
7 Heap / Priority Queue Top-K · Streaming

A heap gives you O(log n) insert and O(1) min/max access. Use a Min-Heap to track the K largest elements (counterintuitive but correct). Use a Max-Heap to track the K smallest.

Classic Example: K Most Frequent Elements

import heapq
from collections import Counter

def topKFrequent(nums, k):
    freq = Counter(nums)
    # Min-heap of size k: (frequency, element)
    heap = []
    for num, count in freq.items():
        heapq.heappush(heap, (count, num))
        if len(heap) > k:
            heapq.heappop(heap)  # remove smallest frequency
    return [item[1] for item in heap]
Time:O(n log k)
Space:O(k)
🎯 Interview Signal "Top K largest/smallest/frequent", "K closest points", "Merge K sorted lists" → Heap Pattern.

Quick Pattern Recognition Guide

Keywords in Problem Pattern to Try
"subarray", "substring", "contiguous", "window"Sliding Window
"sorted array", "pair", "triplet", "palindrome"Two Pointers
"linked list", "cycle", "middle"Fast & Slow Pointers
"shortest path", "level order", "connected"BFS / DFS
"max/min subproblem", "count ways", "can you reach"Dynamic Programming
"minimize maximum", "smallest X that..."Binary Search on Answer
"top K", "K largest", "K closest", "median"Heap / Priority Queue

🚀 Put These Patterns to the Test

Take our 100-question DSA interactive quiz — free, no signup. Test every pattern with real interview-style questions and instant explanations.

Take the DSA Quiz → Get 300Q PDF Bundle

No comments:

Post a Comment

Networking concepts of Data Engineer

Networking for Data Engineers Networking Concepts Every Data Engineer Must Know (2026) You don't need to be a n...

🚫
Content Protected
Copying content from this site is not permitted.
© 2026 InterviewQuestionsToLearn.com