Data Structures & AlgorithmsHardProduct Company SDE · sliding window OA

Problem: Which technique counts subarrays with sum at most K in a non-negative array in O(n)?

  1. A. Sorting the array and binary searching each value
  2. B. Two pointers over a sliding window
  3. C. Dijkstra on prefix sums
  4. D. Floyd-Warshall on indices

Correct answer: B. Two pointers over a sliding window

With non-negative values the window sum is monotone in each pointer move, so the number of valid right ends for each left end is counted in one pass.

More Coding Interview Problems questions

Exams that ask this topic