FastPrepFind First Anagram Index
Problems/Find First Anagram Index

Find First Anagram Index

Databricks logoDatabricks● EasyFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: 🦊 2026-07-18. This problem is backed by a real source. The judged core task matches the source at about 95%.

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.

The function receives:

  • String s, the search string.
  • String pattern, the target anagram pattern.

Return the starting index of the first anagram, or -1 if none exists.

Function

findFirstAnagramIndex(s: String, pattern: String) → int

Examples

Example 1

s = "cbaebabacd"pattern = "abc"return = 0

The substring "cba" starting at index 0 is an anagram of "abc", and it is the first such substring.

Example 2

s = "abab"pattern = "ab"return = 0

The substring "ab" starting at index 0 is already an anagram of the pattern, so the first valid index is 0.

Constraints

  • The source thread did not provide explicit numeric bounds.
  • The anagram must be formed by a contiguous substring of s.
  • If multiple answers exist, return the smallest starting index.

More Databricks problems

  • Rolling Hit CounterPHONE SCREEN · Seen Nov 2026
  • Minimize CommuteOA · PHONE SCREEN · Seen Oct 2026
  • Path Between Two Nodes of a Fibonacci TreeONSITE INTERVIEW · Seen Oct 2026
All Databricks interview questionsSee Databricks hiring insights
Saved
class Solution {
    public int findFirstAnagramIndex(String s, String pattern) {
        // write your code here
    }

}
Checking account…