All Anagram Start Indices
Reported by candidates from Motive's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt on this Motive OA, reported in June 2023, is sorting every window of s and comparing it to sorted p. It passes the three examples and then dies on 100000-character inputs. This is Find All Anagrams in a String: given lowercase s and p, return every start index where a window of s is an anagram of p, overlaps included. The pattern is a fixed-size sliding window over a 26-letter frequency table. If you blank when the clock is running, StealthCoder is the invisible safety net on the live OA. Know the window idea first.
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
The trick: an anagram of p is any substring of length len(p) with identical letter counts. Build a count array of size 26 for p. Slide a window of that same length across s, adding the incoming character and removing the outgoing one. Each step updates one or two counters, so the whole pass is O(n). Track a matches counter, or just compare two 26-length arrays, which is still constant work per step. The common pitfall is rebuilding or sorting the window each time, which turns O(n) into O(n * m log m) and times out at 100000. Other slips: forgetting to return early with an empty list when p is longer than s, and skipping the removal of the leftmost character after the window fills. Overlapping matches are allowed, so never jump the window forward after a hit. If you freeze mid-assessment, StealthCoder can hand you the clean version while you keep control of the submission.
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 Motive's OA.
Motive 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 with a 26-slot frequency array. Window length equals len(p). Each step adds the new right character and drops the old left one, then checks whether the counts equal p's counts. That's linear time with constant extra space.
Why does sorting each window fail?+
With s and p up to 100000 characters, sorting every window costs O(m log m) per position, so the total blows up. The examples are tiny, so it looks fine locally, then times out on the large hidden cases. Counting letters avoids it.
Do overlapping matches count?+
Yes. Example 3 with s = abab and p = ab returns [0,1,2], so all three windows match. Don't skip ahead after finding a match. Just keep sliding one position at a time and record every index where the counts line up.
How hard is this one really?+
Medium. The idea is short once you've seen sliding windows with counts. The difficulty is in the details: window bounds, removing the outgoing character, and the edge case where p is longer than s. Write it once on paper and you're set.
How do I prepare for this in 48 hours?+
Code the sliding window with a 26-length array from scratch twice, then test with the three given examples and a case where len(p) exceeds len(s). Practice the matches-counter variant too. Pay attention to off-by-one errors at the window edges.