Find All Anagrams in a String — Coding Interview Walkthrough
You’re in a coding interview. The interviewer says: “Find all start indices of anagrams of p in s.” This is a fixed-size sliding window with character frequency comparison. As the window slides, you add the incoming character and remove the outgoing one, then check if counts match.
The Problem
Given two strings s and p, return a list of all start indices of p’s anagrams in s.
Example
Input: s = "cbaebabacd", p = "abc"
Output: [0, 6]
Already comfortable with the solution? Practice it in a mock interview →
What Interviewers Are Actually Testing
| Skill | What Interviewers Observe |
|---|---|
| Fixed-size sliding window | Do you slide a window of size len(p) across s? |
| Efficient comparison | Do you use a matches counter for O(1) per step? |
| Add-and-remove | Do you increment/decrement counts correctly? |
Step 1: The Key Insight
A fixed-size sliding window of length len(p). Track a matches counter — the number of characters (out of 26) whose counts currently agree between the window and p. When matches == 26, the window is an anagram.
---
Python Implementation
def findAnagrams(s: str, p: str) -> list[int]:
if len(p) > len(s):
return []
p_count = [0] * 26
s_count = [0] * 26
for c in p:
p_count[ord(c) - ord('a')] += 1
for c in s[:len(p)]:
s_count[ord(c) - ord('a')] += 1
matches = sum(1 for i in range(26) if p_count[i] == s_count[i])
result = [0] if matches == 26 else []
for i in range(len(p), len(s)):
incoming = ord(s[i]) - ord('a')
outgoing = ord(s[i - len(p)]) - ord('a')
s_count[incoming] += 1
if s_count[incoming] == p_count[incoming]:
matches += 1
elif s_count[incoming] == p_count[incoming] + 1:
matches -= 1
s_count[outgoing] -= 1
if s_count[outgoing] == p_count[outgoing]:
matches += 1
elif s_count[outgoing] == p_count[outgoing] - 1:
matches -= 1
if matches == 26:
result.append(i - len(p) + 1)
return result
Time & Space Complexity
| Aspect | Complexity |
|---|---|
| Time | O(n) — O(1) per step |
| Space | O(1) — two 26-element arrays |
Common Interview Mistakes
- Recomputing frequency counts from scratch each window — O(n × L) instead of O(n).
- Not deleting zero-count entries in
Countercomparisons — breaks equality. - Off-by-one in start index. Window ending at
istarts ati - len(p) + 1.
What a Strong Candidate Sounds Like
“Fixed-size sliding window of length
len(p). I track amatchesvariable — how many of the 26 characters have equal counts. When I slide, I update two counts and adjust matches. Whenmatches == 26, it’s an anagram. O(1) per step, O(n) total.”
Related Problems to Practice
- Valid Anagram — Same frequency check, no window.
- Longest Substring Without Repeating Characters — Variable-size sliding window.
- Minimum Window Substring — Harder variable window.
Practice This in a Mock Interview
Start a mock interview for Find All Anagrams in a String on Intervu.
Further reading:
- How to Prepare for a Coding Interview in 2026, the complete roadmap
- Why LeetCode alone isn’t enough, and what to practice instead
- Practice any LeetCode problem as a live mock interview
- The Grind 75 Pathway, a structured study plan with AI practice links
- Browse all walkthroughs on GitHub