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?
- A. 7
- B. 11
- C. 8
- 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.
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 [2, 2, 5, 0, 2, 0, 3, 1, 1, 1] (bar width 1). Using the two-pointer / prefix-max approach for trapping rain water, how much…
- Using a sliding window of fixed size with a character-frequency count, how many substrings of "BAAAABBAACB" are anagrams of "AB"?
- A queue is implemented with two stacks (enqueue pushes to stack "in"; dequeue pops from stack "out", refilling "out" from "in" by…
- A queue is implemented with two stacks (enqueue pushes to stack "in"; dequeue pops from stack "out", refilling "out" from "in" by…
- Floyd's cycle detection runs on a linked list 1 → 2 → ... → 8 whose last node's next pointer points back to node 4 (0-based position 3)…
- Array [36, 53, 71, 81, 12, 18, 20, 26] is a sorted array rotated at pivot 4. Using the standard O(log n) rotated-array binary search…