Data Structures & AlgorithmsVery hardGATE CS · DSA
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 "ABCDBDBCBAB" containing at most k = 2 distinct characters?
- A. 4
- B. 8
- C. 3
- D. 1
Correct answer: A. 4
Sliding the window right and shrinking from the left whenever the distinct-character count exceeds k = 2 gives a longest valid window of length 4. Using k = 1 would give 1, and using k = 3 would give 8 — 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
- Elevation map [3, 4, 1, 3, 4, 6, 3, 5, 4] (bar width 1). Using the two-pointer / prefix-max approach for trapping rain water, how much…
- Heights [9, 2, 9, 6, 1, 6, 3] represent vertical lines at each index. What is the maximum water area (min(height) × horizontal distance)…
- The Dutch National Flag algorithm (single pass, three pointers low/mid/high) partitions [2, 1, 0, 2, 2, 0, 1, 2, 2] (values 0, 1, 2 only)…
- What is the sum of the minimum element over every contiguous subarray of [3, 6, 5, 3, 8, 4, 9, 1]?
- Two sorted linked lists [7 → 13 → 23 → 40] and [5 → 6 → 9 → 18 → 25] are merged with the standard O(n+m) two-pointer merge (always take…
- The Dutch National Flag algorithm (single pass, three pointers low/mid/high) partitions [2, 0, 2, 2, 1, 0, 1, 1] (values 0, 1, 2 only) in…