Data Structures & AlgorithmsHardProduct placement · Amazon/Microsoft
How many contiguous subarrays of [3, −2, 5, 0, −2, 1, 4, 5, 0] have sum exactly 5? (Prefix sums with a hash map of prefix-sum frequencies.)
- A. 6
- B. 5
- C. 2
- D. 1
Correct answer: A. 6
Walk the array keeping prefix sum P and a map of earlier prefixes (with P = 0 seeded once); add freq[P − 5] at every step: total 6. Forgetting the seed gives 5, a two-pointer window (invalid with negatives) finds 2, and counting only prefixes equal to 5 finds 1.
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
- A circular queue in an array of size 5 keeps one slot empty to tell full from empty (front = rear means empty; (rear + 1) mod 5 = front…
- Insertion sort (ascending; shift larger elements right, one key comparison per inner-loop test, the loop stops at the first element ≤ the…
- What is the minimum number of swaps (of any two elements) needed to bring all the 1s of [1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1] together into…
- Can a string become a palindrome by deleting at most one character? Two pointers try skipping either side on the first mismatch. What is…
- Items K, L, M, N, O are pushed onto an empty stack in this order, and a pop may happen at any time. Which pop order is NOT possible?
- Remove the n-th node from the END of a linked list in one pass, using a dummy node and a gap of n between two pointers. What is printed?…