FastPrepFind All Anagrams in a String
Problems/Find All Anagrams in a String

Find All Anagrams in a String

Microsoft logoMicrosoft● MediumINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A review of a Microsoft software engineering internship, lists a find-all-anagrams-in-a-string question. The process had an online assessment and three technical interviews. The review does not say at which step the question came up, and it gives no details of the task itself.

So the details are ours. You return the 0-based start positions of every window of s whose letters are a rearrangement of p, in increasing order. Windows may overlap, an empty array means there are none, and both strings are lowercase with up to 2 * 10^4 letters.

Our version is about 95% the same as the reported question.

Given two strings s and p, return the start indices (0-based) of every substring of s that is an anagram of p, in increasing order.

An anagram uses exactly the same letters with the same counts, in any order. A substring is a run of characters that are next to each other in s, so each answer is a window of length p.length. Windows may overlap. If there are none, return an empty array.

Function

findAnagrams(s: String, p: String) → List<Integer>

Examples

Example 1

s = "cbaebabacd"p = "abc"return = [0,6]

The window starting at 0 is cba and the window starting at 6 is bac; both use the letters a, b and c once each. No other window of length 3 does, so the answer is [0, 6].

Example 2

s = "abab"p = "ab"return = [0,1,2]

The windows are ab (start 0), ba (start 1) and ab (start 2). All three are anagrams of ab, and windows may overlap.

Example 3

s = "ab"p = "abc"return = []

p is longer than s, so no window fits and the answer is empty.

Constraints

  • 1 <= s.length, p.length <= 2 * 10^4
  • s and p consist of lowercase English letters.

More Microsoft Onsite Interview questions

  • Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
  • Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
  • Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
  • Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
  • Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
  • Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
  • Coin ChangeONSITE INTERVIEW · Seen Oct 2026
  • Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
  • Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
  • Totals from a Tiered Rate TableONSITE INTERVIEW · Seen Oct 2026

Similar questions at other companies

  • Longest Substring Without Repeating CharactersSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Shortest Deletion for Distinct CharactersOracle · ONSITE INTERVIEWOracle interview questions
  • Longest Substring Without Repeating CharactersGoogle · ONSITE INTERVIEWGoogle interview questions
  • Longest Substring Without Repeating CharactersAmazon · ONSITE INTERVIEW
  • Longest Substring Without Repeating CharactersInfosys · ONSITE INTERVIEWInfosys interview questions
  • Longest Substring Without Repeating CharactersOmnissa · ONSITE INTERVIEWOmnissa interview questions
  • Longest Substring with At Most K Distinct CharactersGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
  • Longest Substring Without Repeating CharactersBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
  • Sliding-Window Rate LimiterStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
  • Longest Substring Without Repeated CharactersDatadog · ONSITE INTERVIEWDatadog interview questions

Interview reports that mention this question

  • Microsoft SWE Intern, Early 2026: 54 Experiences (Part 2 of 6)
  • Microsoft SWE Intern Interviews: 244 Experiences (Part 1 of 6)

Microsoft interview experiences

  • Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
  • Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
  • Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
  • Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)
All Microsoft interview questionsSee Microsoft hiring insights
Saved
class Solution {
    public List<Integer> findAnagrams(String s, String p) {
        // Write your code here.
    }
}
Checking account…