📖 Scenario: Imagine you have a list of daily sales numbers for a small shop. You want to quickly find out the total sales from the start up to any given day without adding all the numbers every time.
🎯 Goal: You will build a prefix sum array that stores the running total of sales up to each day. This helps answer total sales queries quickly.
📋 What You'll Learn
Create an array called sales with exact daily sales numbers
Create an empty array called prefixSums to store running totals
Use a for loop with variable i to calculate prefix sums
Print the prefixSums array at the end
💡 Why This Matters
🌍 Real World
Prefix sums are used in finance, gaming, and data analysis to quickly calculate totals over ranges without repeated addition.
💼 Career
Understanding prefix sums helps in coding interviews and real-world tasks involving efficient data queries.
Progress0 / 4 steps
1
Create the sales data array
Create an array called sales with these exact daily sales numbers: 10, 20, 15, 5, 30.
DSA Typescript
Hint
Use square brackets [] to create the array and separate numbers with commas.
2
Create the prefix sums array
Create an empty array called prefixSums to store the running totals of sales.
DSA Typescript
Hint
Use [] to create an empty array and specify the type as number[].
3
Calculate prefix sums using a loop
Use a for loop with variable i from 0 to sales.length - 1. Inside the loop, add the current sale to the previous prefix sum (or 0 if i is 0) and store it in prefixSums[i].
DSA Typescript
Hint
Use a conditional expression to add the previous prefix sum only if i is greater than 0.
4
Print the prefix sums array
Print the prefixSums array using console.log(prefixSums).
DSA Typescript
Hint
The output should show the running totals: 10, 30, 45, 50, 80.
Practice
(1/5)
1. What is the main purpose of a prefix sum array in algorithms?
easy
A. To quickly find the sum of elements between any two indices
B. To sort the array elements in ascending order
C. To reverse the array elements
D. To find the maximum element in the array
Solution
Step 1: Understand what prefix sum array stores
A prefix sum array stores cumulative sums of elements from the start up to each index.
Step 2: Identify the use of prefix sums
Using prefix sums, we can find the sum of any subarray quickly by subtracting two prefix sums.
Final Answer:
To quickly find the sum of elements between any two indices -> Option A
Quick Check:
Prefix sum array = fast range sum queries [OK]
Hint: Prefix sums help answer range sum queries fast [OK]
Common Mistakes:
Confusing prefix sum with sorting
Thinking prefix sum reverses array
Assuming prefix sum finds max element
2. Which of the following TypeScript code snippets correctly initializes a prefix sum array prefix for an input array arr?
A. prefix array is not initialized with 0 at index 0
B. Loop runs one time too many
C. arr[i] is accessed out of bounds
D. prefix array is declared with wrong type
Solution
Step 1: Check prefix array initialization
The prefix array is empty initially, so prefix[0] is undefined.
Step 2: Identify impact on prefix sum calculation
prefix[i] is undefined for i=0, so prefix[1] = undefined + arr[0] results in NaN.
Final Answer:
prefix array is not initialized with 0 at index 0 -> Option A
Quick Check:
Initialize prefix[0] = 0 to avoid NaN [OK]
Hint: Always start prefix array with 0 at index 0 [OK]
Common Mistakes:
Not initializing prefix[0]
Assuming empty array has prefix[0]
Ignoring NaN results
5. You have an array arr = [3, 1, 4, 1, 5, 9, 2]. Using a prefix sum array, how would you efficiently find the sum of elements from index 2 to 5 (inclusive)?
hard
A. Calculate prefix sums, then sum = prefix[5] - prefix[1]
B. Calculate prefix sums, then sum = prefix[6] - prefix[1]
C. Calculate prefix sums, then sum = prefix[5] - prefix[2]
D. Calculate prefix sums, then sum = prefix[6] - prefix[2]
Solution
Step 1: Understand prefix sum indexing
Prefix sum array prefix has prefix[0] = 0, prefix[i] = sum of arr[0] to arr[i-1].
Step 2: Calculate sum from index 2 to 5
Sum = prefix[6] - prefix[2], because prefix[6] sums arr[0..5], prefix[2] sums arr[0..1].
Final Answer:
Calculate prefix sums, then sum = prefix[6] - prefix[2] -> Option D
Quick Check:
Sum of arr[2..5] = prefix[6] - prefix[2] [OK]
Hint: Sum(i to j) = prefix[j+1] - prefix[i] [OK]
Common Mistakes:
Off-by-one errors in prefix indices
Subtracting wrong prefix sums
Confusing inclusive/exclusive ranges
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.