Data Structures & AlgorithmsModerateTCS NQT · Accenture

Problem: Count the occurrences of the pattern "aaa" in the text "aaabaaaab", where occurrences may overlap (each starting index that matches counts once).

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

Correct answer: C. 3

Overlapping matches start at indices 0, 4 and 5, so there are 3. Non-overlapping scanning (jumping past each match) finds only 2.

More Coding Interview Problems questions

Exams that ask this topic