Data Structures & AlgorithmsVery hard
In a sliding window tracking the maximum number of vowels among windows of fixed length 3, why is O(1) update per step possible?
- A. Vowels can be counted by sorting the window each time.
- B. The window must be rebuilt from scratch each step.
- C. Moving the window by one changes only one entering and one leaving character, so the vowel count changes by at most one.
- D. Vowel counts are always equal across windows.
Correct answer: C. Moving the window by one changes only one entering and one leaving character, so the vowel count changes by at most one.
Only two characters change between consecutive windows, so the count is updated with two checks. Rebuilding the count from scratch would cost the full window length each time.
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
- Why does a circular window of length 3 require index arithmetic modulo n?
- Why is the number of Dyck-word stack sequences with n pushes and n pops equal to a Catalan number?
- A deque-based minimum of each sliding window runs in linear time because:
- How many ways are there to make 15 using coins of values 1, 2, 5 and 10, where the order of coins does not matter (so 5+5+5 and 5+10 are…
- A binary string "1011001010" should become non-decreasing (all 0s before all 1s). In one operation you flip a single character. What is…
- What is the length of the longest NON-DECREASING subsequence (equal neighbours allowed) of [5, 1, 3, 3, 2, 6, 6, 4, 7]?