Data Structures & AlgorithmsHardProduct placement · Amazon/Microsoft
Which problem CANNOT be solved by a plain variable-size sliding window that shrinks from the left?
- A. Longest subarray with sum exactly k when the array may contain negative numbers
- B. Longest substring with at most k distinct characters
- C. Shortest subarray with sum ≥ k when all numbers are positive
- D. Longest substring without repeating characters
Correct answer: A. Longest subarray with sum exactly k when the array may contain negative numbers
Shrinking works only when the window property is monotone (adding elements never 'fixes' a violated window). With negative numbers, extending can lower the sum, so a prefix-sum hash map is needed instead.
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 Boyer–Moore majority vote (candidate/counter, no verification pass) runs on [3, 5, 5, 7, 7, 7, 7, 7, 3]. What candidate and final…
- KMP failure function (lps[i] = length of the longest proper prefix of p[0..i] that is also a suffix). What is printed? python def lps(p)…
- The array [1, 2, 3, 4, 5, 6, 7, 8] is rotated RIGHT by 3 positions and then LEFT by 2 positions. What is the final array?
- Kadane's algorithm runs on [4, 0, 8, −3, −2, −2, −3, 2, −6]. What is the maximum sum of a non-empty contiguous subarray?
- How many characters must be deleted in total from two strings so that they become anagrams of each other? What is printed? python from…
- What does this loop-counting code print? (Python) python n, c = 33, 0 i = 1 while i < n: j = i while j < n: c += 1 j = 2 i += 1 print(c)