Data Structures & AlgorithmsVery hardAmazon/Microsoft SDE pattern

Using a sliding window of fixed size with a character-frequency count, how many substrings of "BAAAABBAACB" are anagrams of "AB"?

  1. A. 1
  2. B. 4
  3. C. 2
  4. D. 3

Correct answer: D. 3

A window of length 2 is an anagram of "AB" exactly when their character-frequency counts match, not when the characters appear in the same order. Checking every window of "BAAAABBAACB" this way gives 3 matches, versus only 1 if exact substring equality (same order) were required instead.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic