Data Structures & AlgorithmsHardHCLTech · Wipro pattern

Using a sliding window (shrink from the left whenever more than k distinct characters are in the window), what is the length of the longest substring of "BACBBACDACDA" containing at most k = 3 distinct characters?

  1. A. 12
  2. B. 7
  3. C. 6
  4. D. 3

Correct answer: B. 7

Sliding the window right and shrinking from the left whenever the distinct-character count exceeds k = 3 gives a longest valid window of length 7. Using k = 2 would give 3, and using k = 4 would give 12 — off-by-one errors on the distinct-count bound.

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

Exams that ask this topic