
Arrays are the “Bread and Butter” of coding. Every interview starts here.
But most candidates fail not because they don’t know loop syntax, but because they use Brute Force ($O(N^2)$) instead of Pattern Logic ($O(N)$).
To clear top-tier interviews, you must stop thinking about “Loops” and start thinking about “Pointers and Windows.” This guide covers the 12 Essential Array Patterns that map to 90% of LeetCode problems.
Part 1: The “Pointer” Patterns (Navigation)
How to move through an array efficiently.
1. Two Pointers (Converging)
- The Scenario: “Find a pair that sums to Target X” or “Reverse the array.”
- The Logic: One pointer at Start, one at End. Move them toward each other based on a condition.
- Complexity: $O(N)$.
2. Fast & Slow Pointers (Tortoise & Hare)
- The Scenario: “Find the Duplicate Number” (Read-only array) or “Cycle Detection.”
- The Logic: Use array values as indices.
Fastmoves 2 steps,Slowmoves 1 step. - Why: This solves the “Duplicate” problem in $O(N)$ time and $O(1)$ space without modifying the array.
Part 2: The “Window” Patterns (Sub-arrays)
Focusing on a specific chunk of the array.
3. Sliding Window (Fixed & Variable)
- The Scenario: “Max sum of subarray of size K” or “Smallest subarray with sum > S.”
- The Logic:
- Add element to the right (expand).
- Remove element from the left (shrink) if condition is broken.
- Trigger: “Contiguous Subarray”, “Longest Substring.”
4. Kadane’s Algorithm (Max Subarray Sum)
- The Scenario: “Find the contiguous subarray with the largest sum.”
- The Logic:
- Carry the
current_sum. - If
current_sumbecomes negative, reset it to 0 (because a negative prefix never helps). - Update
max_so_far.
- Carry the
Part 3: The “Pre-Computation” Pattern (Range Queries)
Answering “How many?” or “What is the sum?” instantly.
5. Prefix Sum Array
- The Scenario: “Calculate sum of elements between index
LandRmultiple times.” - The Logic: Create an auxiliary array where
P[i] = Sum(0...i).Sum(L, R) = P[R] - P[L-1].
- Complexity: Query takes $O(1)$.
Part 4: The “Sorting” Patterns (Ordering)
Sorting without std::sort.
6. Dutch National Flag (Sort Colors)
- The Scenario: “Sort an array of 0s, 1s, and 2s in one pass.”
- The Logic: Use 3 pointers:
Low(for 0s),Mid(for 1s),High(for 2s).- 0? Swap to Low.
- 1? Leave it (Mid++).
- 2? Swap to High.
7. Cyclic Sort (Missing/Duplicate)
- The Scenario: “Array contains numbers 1 to N. Find the missing/duplicate.”
- The Logic: Put number
xat indexx-1.- If
nums[i] != nums[nums[i]-1], swap them. - Do this until everyone is in their “chair.”
- If
- Trigger: “Numbers from 1 to N.”
Part 5: The “Voting” Pattern (Counting)
Finding the leader without a Hash Map.
8. Moore’s Voting Algorithm
- The Scenario: “Find the Majority Element (appears > N/2 times).”
- The Logic:
- Assume first number is leader.
Count = 1. - If next number is same,
Count++. If different,Count--. - If
Count == 0, pick new leader.
- Assume first number is leader.
- Why: The majority element will survive the “cancellations.”
Part 6: The “Search” Pattern (Dividing)
Navigating sorted or rotated data.
9. Binary Search (Standard & Rotated)
- The Scenario: “Find target in Rotated Sorted Array.”
- The Logic: Find which half is sorted. Check if target lies in that range. If yes, go there. If no, go to the other half.
Part 7: The “Matrix” Patterns (2D Arrays)
Don’t get lost in the grid.
10. Rotate Image (In-place)
- The Scenario: “Rotate a Matrix 90 degrees clockwise.”
- The Logic:
- Transpose the matrix (swap
[i][j]with[j][i]). - Reverse every row.
- Transpose the matrix (swap
11. Spiral Matrix
- The Scenario: “Print matrix in spiral order.”
- The Logic: Maintain 4 boundaries:
Top,Bottom,Left,Right.- Move Right, Move Down, Move Left, Move Up.
- Shrink boundaries after every pass.
12. Search in 2D Matrix (Staircase Search)
- The Scenario: Rows are sorted, Columns are sorted. Find target.
- The Logic: Start at Top-Right corner.
- If
Target < Current, go Left. - If
Target > Current, go Down.
- If
The Professor’s Cheat Code Sheet
Stick this table at the bottom. It maps the Constraint to the Algorithm.
| If the problem mentions… | Use this Algorithm | Complexity |
| Sorted Array + Target Sum | Two Pointers | $O(N)$ |
| Contiguous Subarray + Max/Min | Sliding Window | $O(N)$ |
| Contains Negatives + Max Sum | Kadane’s Algo | $O(N)$ |
| Range Sum Queries (L to R) | Prefix Sum | $O(1)$ Query |
| Numbers range 1 to N | Cyclic Sort | $O(N)$ |
| Majority (> N/2) | Moore’s Voting | $O(N)$ |
| Sort 0s, 1s, 2s | Dutch National Flag | $O(N)$ |
| Rotated Sorted Array | Binary Search | $O(\log N)$ |
| Matrix + Sorted Rows/Cols | Staircase Search | $O(N+M)$ |
Professor’s Advice for Teaching This
“Students, Arrays are not just about
forloops.
- Level 1: Master Two Pointers and Sliding Window. This clears 60% of interviews.
- Level 2: Master Kadane’s and Cyclic Sort. This clears Product Company rounds.
- Level 3: Master Moore’s Voting and Matrix Manipulation. This makes you ‘Google Ready’.”
The Complete Array Algorithms Roadmap: 12 Patterns from Beginner to Expert (2026 Guide)
Array Algorithms for Interview Kadane’s Algorithm Explained Sliding Window vs Two Pointers Dutch National Flag Problem
YouTube Channels:
Trendy VS Vlogs
VS Coding Academy
Join Our WhatsApp Channel for the latest job opportunities and updates:
VS_CODING_ACADEMY WhatsApp Channel
Join Our Telegram Channel for the latest job opportunities and updates: https://t.me/vscodingacademy
Open our site in Telegram Bot: https://t.me/vscodingacademy_bot
For DSA Guide: https://vscodingacademy.com/category/dsa-guide/