Engrain by ByteRush

DSA.06.03 Mid-levelCore — asked often

Pattern 3: Prefix sums

Comes up in: DSA

This concept is being written. It gets the full five steps — explanation, predict, interview angle, practice with tests, and review cards. Here is exactly what it will cover, so you can study it elsewhere in the meantime.

What it covers

  • prefix array
  • range queries in O(1)
  • prefix sum + hash map for "subarray sum = k"
  • prefix products
  • 2D prefix sums

Words you will know

prefix sum

What you will practise

  • range-sum-query (E)
  • product-except-self (M)
  • subarray-sum-equals-k (M)
  • contiguous-binary-array (M)

Interview questions this answers

  • Find the number of subarrays whose sum is k in O(n).