All Anagram Start Indices
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon reported this one in September 2026, and the whole problem hinges on a frequency table. Find every index in s where a window is an anagram of p. It's a fixed-size sliding window, and the data structure that makes it fast is a 26-slot count array. If you've got an Amazon OA coming in the next day or two, expect this shape: lowercase letters, overlapping matches, inputs up to 100000. Brute force sorting every window will time out. StealthCoder sits invisibly as a safety net on the live OA if your mind goes blank, but the pattern below is short enough to own tonight.
The problem
Given lowercase strings s and p, return every starting index where a substring of s is an anagram of p. Return indices in increasing order. Overlapping matches are included. Function findAnagrams(s: String, p: String) → int[] Examples Example 1 s = "acbadabcaa" p = "aabc" return = [0,5,6] The length-four substrings at 0, 5, and 6 have exactly the pattern frequencies. Example 2 s = "cbaebabacd" p = "abc" return = [0,6] cba and bac are anagrams of abc. Example 3 s = "abab" p = "ab" return = [0,1,2] All three length-two windows match, including overlaps. Constraints 1 ≤ s.length, p.length ≤ 100000. s and p contain only lowercase English letters.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Build a count array of size 26 for p. Slide a window of length p.length across s, adding the incoming character and removing the outgoing one. When the window counts equal the pattern counts, record the left index. Comparing two 26-length arrays each step is O(26), so the total is O(n), which is fine for 100000. The cleaner trick is a single 'matches' or 'need' counter so each step is O(1). Common pitfalls: forgetting to return when p is longer than s, removing the wrong character when the window slides, and sorting each substring, which blows up the runtime. Overlaps are allowed, so don't skip ahead after a hit. Example 3, abab with ab giving [0,1,2], is a good test. If you freeze during the live OA, StealthCoder can surface the window logic, but write the count-array version once by hand first.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill All Anagram Start Indices cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as find all anagrams in a string. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
All Anagram Start Indices FAQ
What's the trick to All Anagram Start Indices?+
Use a fixed-size sliding window of length p.length with a 26-slot frequency array. Add the new right character, remove the old left one, and compare counts to the pattern. Every match records the left index. No sorting, no regenerating substrings.
How hard is this one really?+
Medium. The idea is simple once you see the window, but off-by-one errors on the slide are where people lose time. With s and p up to 100000, anything slower than linear fails, so the approach matters more than the code.
Do overlapping matches count?+
Yes. Example 3 with s = abab and p = ab returns [0,1,2]. Don't jump the window forward after a match. Slide one character at a time and check every position.
Should I compare arrays or track a match counter?+
Comparing two 26-length arrays per step is fine and easy to get right, giving O(26n). A match counter gives true O(n) but is easier to bug. Pick the array compare unless you're confident with the counter.
How do I prepare in 48 hours for an Amazon OA like this?+
Write this problem from scratch twice, then do two or three related fixed-window string problems. Practice the edge cases: p longer than s, single-character strings, and all-same-letter inputs. Focus on the pattern, not memorizing code.