Data Structures & AlgorithmsHardGoldman Sachs OA · LeetCode medium-hard

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 "BDACCCDDBBB" containing at most k = 3 distinct characters?

  1. A. 7
  2. B. 11
  3. C. 8
  4. D. 5

Correct answer: C. 8

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

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

Exams that ask this topic