22 Essential DSA Patterns for Coding Interviews

Featured Image
Mastering Data Structures and Algorithms (DSA) is crucial for cracking technical interviews. Below are 22 DSA patterns that every programmer should know, along with practical implementations and commands to reinforce learning.

1. Fast and Slow Pointer

  • Use Case: Cycle detection in linked lists.
  • Linux Command: Use `strace` to trace system calls in a process (similar to pointer traversal).
    strace -p <PID> 
    

2. Merge Intervals

  • Use Case: Scheduling, calendar apps.
  • Python Example:
    def merge(intervals):
    intervals.sort(key=lambda x: x[bash])
    merged = []
    for interval in intervals:
    if not merged or merged[-1][bash] < interval[bash]:
    merged.append(interval)
    else:
    merged[-1][bash] = max(merged[-1][bash], interval[bash])
    return merged
    

3. Sliding Window

  • Use Case: Substring problems (e.g., longest substring without repeating characters).
  • Bash Script Example:
    !/bin/bash
    arr=(1 2 3 4 5)
    window_size=3
    for ((i=0; i<=${arr[@]}-window_size; i++)); do
    echo "${arr[@]:i:window_size}"
    done
    

4. Islands (Matrix Traversal – DFS/BFS)

  • Use Case: Number of islands in a grid.
  • Linux Command: Simulate grid traversal with `find` (recursive file search).
    find /path -type f -name ".txt" 
    

5. Two Pointers

  • Use Case: Pair sum, palindrome checks.
  • Python Example:
    def two_sum(nums, target):
    left, right = 0, len(nums)-1
    while left < right:
    current_sum = nums[bash] + nums[bash]
    if current_sum == target:
    return [left, right]
    elif current_sum < target:
    left += 1
    else:
    right -= 1
    return []
    

6. Cyclic Sort

  • Use Case: Sorting arrays with numbers in a given range.
  • Linux Command: `sort` utility for file sorting.
    sort -n input.txt > output.txt 
    

7. In-place Reversal of Linked List

  • Use Case: Reverse a linked list without extra space.
  • Bash Script Example:
    reverse_array() {
    local arr=("$@")
    echo "${arr[@]}" | rev
    }
    reverse_array 1 2 3 4 
    

8. Breadth First Search (BFS)

  • Use Case: Shortest path in unweighted graphs.
  • Linux Command: `tree` for BFS-like directory traversal.
    tree -L 2 
    

9. Depth First Search (DFS)

  • Use Case: Maze solving, topological sorting.
  • Linux Command: `find` with depth limit.
    find /path -maxdepth 3 -type f 
    

10. Two Heaps (Min & Max Heaps)

  • Use Case: Median in a data stream.
  • Python Example:
    import heapq
    min_heap = []
    max_heap = []
    

11. Subsets (Backtracking)

  • Use Case: Generate all possible subsets.
  • Linux Command: Generate combinations using `combinations` in Python.
    python3 -c "from itertools import combinations; print(list(combinations([1,2,3], 2)))" 
    

12. Modified Binary Search

  • Use Case: Searching in rotated arrays.
  • Linux Command: `grep` for binary-like search in logs.
    grep -i "error" /var/log/syslog 
    

13. Bitwise XOR

  • Use Case: Finding unique numbers.
  • Bash Example:
    echo $((5 ^ 3))  Output: 6 
    

14. Top ‘K’ Elements

  • Use Case: Finding top K frequent elements.
  • Linux Command: `sort | uniq -c | sort -nr` for frequency counting.
    cat file.txt | tr ' ' '\n' | sort | uniq -c | sort -nr 
    

15. K-way Merge

  • Use Case: Merging K sorted lists.
  • Linux Command: Merge sorted files.
    sort -m file1.txt file2.txt > merged.txt 
    

16. 0/1 Knapsack (DP)

  • Use Case: Maximizing value with weight constraints.
  • Python Example:
    def knapsack(W, wt, val, n):
    dp = [<a href="W+1">0</a> for _ in range(n+1)]
    for i in range(n+1):
    for w in range(W+1):
    if i == 0 or w == 0:
    dp[bash][w] = 0
    elif wt[i-1] <= w:
    dp[bash][w] = max(val[i-1] + dp[i-1][w-wt[i-1]], dp[i-1][bash])
    else:
    dp[bash][w] = dp[i-1][bash]
    return dp[bash][bash]
    

17. Unbounded Knapsack (DP)

  • Use Case: Infinite item selection.
  • Linux Command: Simulate repetition with loops.
    for i in {1..5}; do echo "Iteration $i"; done 
    

18. Topological Sort (Graphs)

  • Use Case: Task scheduling.
  • Linux Command: `tsort` for topological sorting.
    echo -e "A B\nB C" | tsort 
    

19. Monotonic Stack

  • Use Case: Next greater element.
  • Python Example:
    stack = []
    for num in nums:
    while stack and stack[-1] < num:
    print(stack.pop(), "->", num)
    stack.append(num)
    

20. Backtracking

  • Use Case: N-Queens, Sudoku.
  • Linux Command: Recursive file search with find.
    find / -name ".conf" 
    

21. Union Find (Disjoint Set)

  • Use Case: Network connectivity.
  • Python Example:
    parent = [i for i in range(n)]
    def find(u):
    while parent[bash] != u:
    parent[bash] = parent[parent[bash]]
    u = parent[bash]
    return u
    

22. Greedy Algorithm

  • Use Case: Minimum coins problem.
  • Linux Command: `cut` for greedy text extraction.
    cut -d',' -f1 file.csv 
    

What Undercode Say

Mastering these 22 DSA patterns ensures strong problem-solving skills for coding interviews. Practice with Linux commands, Python scripts, and Bash automation to reinforce learning.

Expected Output:

  • DSA Patterns Guide: https://lnkd.in/dte69Z5N
  • Prediction: Increased demand for DSA + System Design skills in 2024 interviews.

References:

Reported By: Rajatgajbhiye Clear – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

💬 Whatsapp | 💬 Telegram