Data Structures & AlgorithmsHardAmazon/Microsoft SDE 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 "DBDADBABA" containing at most k = 2 distinct characters?
- A. 4
- B. 1
- C. 3
- D. 9
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 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
- A circular queue of capacity 4 starts empty and runs this sequence in order: enqueue(8), enqueue(2), enqueue(5), dequeue(), enqueue(1)…
- Binary search for first/last occurrence runs on the sorted array [1, 3, 7, 9, 11, 13, 15, 15, 16, 17, 18] for target 15. What (first…
- An LRU cache of capacity 3 runs this sequence of operations in order: put(8, 80), put(8, 80), put(6, 60), put(7, 70), put(8, 80), put(1…
- Kadane's algorithm runs on the array [2, -8, 4, -2, 4, -5, -2] to find the maximum sum of a contiguous subarray. What sum does it return?
- What is the postfix (Reverse Polish) form of the infix expression "j + e f -(b + a)", built with the standard stack-based conversion (^…
- What is the sum of the minimum element over every contiguous subarray of [8, 1, 5, 4, 4, 2]?