Find First Anagram Index
Reported by candidates from Databricks's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Databricks hit you with a sliding window anagram search in April 2026. You need to find where in string s a substring matches pattern as an anagram, then return that index or -1. This is a hash-table pattern disguised as a string problem. The trick is keeping a frequency map of pattern and sliding a window across s, checking when the window's character counts match. StealthCoder will catch the off-by-one errors and forgotten edge cases when you're live.
The problem
You are given two strings s and pattern. Find the starting index of the first substring of s that is an anagram of pattern. If no substring of s is an anagram of pattern, return -1. Two strings are anagrams if they contain the same characters with the same frequencies. Function Description Complete the function findFirstAnagramIndex in the editor below. findFirstAnagramIndex has the following parameters: Returns The source thread did not provide explicit numeric bounds.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Build a frequency map for pattern first. Then slide a window of pattern.length across s, maintaining a frequency map for the current window. When both maps match, return the window's starting index. The pitfall: most candidates rebuild the entire frequency map per window instead of using a rolling update (remove left char, add right char). That's O(n * m * 26) instead of O(n). Use a helper function to check if two frequency maps are equal, or count matching keys instead of deep comparison. StealthCoder handles the bookkeeping when you're tired on the live OA.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Find First Anagram Index 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. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
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 Databricks's OA.
Databricks reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Find First Anagram Index FAQ
Do I need to sort or is frequency matching enough?+
Frequency matching is enough. Anagrams have identical character counts, not order. Sorting would work but it's slower. Build two maps and compare keys and values. That's the intended path.
What if pattern is longer than s?+
Return -1. Your window can't slide if s is shorter than pattern. Check this before you enter the loop or you'll waste time on edge case bugs.
Should I rebuild the frequency map every iteration?+
No. Slide the window: remove the leftmost character from the old window, add the new rightmost character. That keeps you O(n) instead of O(n*m). Most candidates miss this and still pass with small inputs.
How do I check if two frequency maps match?+
Compare the maps directly, or track how many characters have matching frequencies. If you increment a counter when a character's frequency becomes equal and decrement when it diverges, you can check if the counter equals the alphabet size.
Is this still a common OA question?+
Yes. Anagram and sliding window questions appear everywhere. This is a mid-tier problem that feels harder than it is once you see the pattern. Practice the rolling window update once and you own it.