Data Structures & AlgorithmsVery hardProduct company OA
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 "BCAABBCCC" containing at most k = 2 distinct characters?
- A. 4
- B. 9
- C. 5
- D. 3
Correct answer: C. 5
Sliding the window right and shrinking from the left whenever the distinct-character count exceeds k = 2 gives a longest valid window of length 5. Using k = 1 would give 3, and using k = 3 would give 9 — off-by-one errors on the distinct-count bound.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- The Dutch National Flag algorithm (single pass, three pointers low/mid/high) partitions [2, 0, 0, 1, 0, 1, 1, 0] (values 0, 1, 2 only) in…
- Array [87, 4, 31, 36, 38, 63, 75] is a sorted array rotated at pivot 6. Using the standard O(log n) rotated-array binary search (decide…
- Array [56, 73, 74, 82, 90, 3, 48] is a sorted array rotated at pivot 2. Using the standard O(log n) rotated-array binary search (decide…
- The Boyer–Moore majority vote algorithm (candidate/counter, no verification pass) runs on [1, 5, 5, 2, 5, 1, 5, 5]. What candidate and…
- The Boyer–Moore majority vote algorithm (candidate/counter, no verification pass) runs on [6, 4, 4, 4, 6, 4, 6, 2]. What candidate and…
- Using a sliding window (shrink from the left whenever more than k distinct characters are in the window), what is the length of the…