Data Structures & AlgorithmsVery hardGoldman 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 "DBCABCCBBB" containing at most k = 2 distinct characters?
- A. 9
- B. 5
- C. 6
- D. 3
Correct answer: C. 6
Sliding the window right and shrinking from the left whenever the distinct-character count exceeds k = 2 gives a longest valid window of length 6. 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 [1, 2, 0, 0, 2, 1, 0, 2, 0] (values 0, 1, 2 only)…
- Using a sliding window (shrink from the left whenever more than k distinct characters are in the window), what is the length of the…
- The Boyer–Moore majority vote algorithm (candidate/counter, no verification pass) runs on [3, 4, 4, 4, 4, 1, 3, 4, 4]. What candidate and…
- A recursive function is defined as f(0) = f(1) = 1 and f(n) = f(n−1) + 2·f(n−2) for n ≥ 2. Using memoization, what is f(9)?
- The Dutch National Flag algorithm (single pass, three pointers low/mid/high) partitions [1, 0, 2, 2, 2, 1, 1, 0] (values 0, 1, 2 only) in…
- What is the postfix (Reverse Polish) form of the infix expression "e (a + b)- h / d", built with the standard stack-based conversion (^…