Data Structures & AlgorithmsHardProduct placement · Amazon/Microsoft

Which problem CANNOT be solved by a plain variable-size sliding window that shrinks from the left?

  1. A. Longest subarray with sum exactly k when the array may contain negative numbers
  2. B. Longest substring with at most k distinct characters
  3. C. Shortest subarray with sum ≥ k when all numbers are positive
  4. 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.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic