Data Structures & AlgorithmsModerateTCS NQT · Infosys

Problem: Find the length of the shortest substring of "bbaac" that contains both letters 'a' and 'b' at least once.

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

Correct answer: B. 2

The shortest window containing both letters sits at the boundary between the b-run and the a-run. Scanning only from the start of the string misses the window that begins at the last b.

More Coding Interview Problems questions

Exams that ask this topic