Data Structures & AlgorithmsHardTCS NQT · Accenture

Problem: The string "aabcbbca" is processed by a sliding window that keeps at most one distinct letter 'a' inside (other letters are allowed only outside). What is the longest substring containing at most one 'b' and any number of other letters?

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

Correct answer: D. 4

The window shrinks from the left only when a second 'b' enters, which keeps the invariant. Ignoring the left-side shrink grows the window past the allowed count.

More Coding Interview Problems questions

Exams that ask this topic