📖 Scenario: You are working with a list of daily sales numbers for a small shop. You want to quickly find the total sales up to any given day without adding all previous days every time.
🎯 Goal: Create a prefix sum array that stores the running total of sales up to each day. This will help you answer total sales queries efficiently.
📋 What You'll Learn
Create an array called sales with exact daily sales values
Create a prefix sum array called prefixSums to store running totals
Use a loop to fill prefixSums based on sales
Print the prefixSums array as the final output
💡 Why This Matters
🌍 Real World
Prefix sums are used in finance, data analysis, and gaming to quickly calculate sums over ranges without repeated addition.
💼 Career
Understanding prefix sums helps in optimizing algorithms and is a common topic in coding interviews and software development.
Progress0 / 4 steps
1
Create the sales data array
Create an array called sales with these exact daily sales numbers: 5, 8, 6, 3, 10.
DSA Javascript
Hint
Use square brackets [] to create an array and separate numbers with commas.
2
Create the prefix sum array with initial value
Create an array called prefixSums with the same length as sales and set the first element prefixSums[0] equal to sales[0].
DSA Javascript
Hint
Use new Array(length) to create an empty array of the same size.
3
Fill the prefix sum array using a loop
Use a for loop with variable i starting from 1 up to sales.length - 1. Inside the loop, set prefixSums[i] equal to prefixSums[i - 1] + sales[i].
DSA Javascript
Hint
Start the loop at 1 because the first prefix sum is already set.
4
Print the prefix sum array
Print the prefixSums array using console.log(prefixSums).
DSA Javascript
Hint
The output should be the array of running totals.
Practice
(1/5)
1. What does a prefix sum array represent for a given array arr?
easy
A. An array where each element is the sum of all elements from the start up to that index in arr.
B. An array where each element is the product of all elements from the start up to that index in arr.
C. An array where each element is the difference between consecutive elements in arr.
D. An array where each element is the maximum value found in arr up to that index.
Solution
Step 1: Understand prefix sum array definition
A prefix sum array stores sums of elements from the start of the original array up to each index.
Step 2: Compare options with definition
Only An array where each element is the sum of all elements from the start up to that index in arr. correctly describes this sum accumulation behavior.
Final Answer:
An array where each element is the sum of all elements from the start up to that index in arr. -> Option A
Quick Check:
Prefix sum = cumulative sum [OK]
Hint: Prefix sum means cumulative addition from start [OK]
Common Mistakes:
Confusing prefix sum with product or max
Thinking prefix sum stores differences
Assuming prefix sum stores original values
2. Which of the following JavaScript code snippets correctly initializes a prefix sum array prefix for an input array arr?
A. prefix array is not initialized with first element, causing prefix[-1] to be undefined.
B. The loop should start from i = 1 instead of 0.
C. The code uses addition instead of multiplication.
D. The array arr is empty, so prefix will be empty.
Solution
Step 1: Analyze prefix[-1] usage at i=0
At i=0, prefix[i-1] is prefix[-1], which is undefined, causing NaN result.
Step 2: Fix initialization to avoid undefined
Initialize prefix[0] = arr[0] before loop or handle i=0 separately.
Final Answer:
prefix array is not initialized with first element, causing prefix[-1] to be undefined. -> Option A
Quick Check:
Accessing prefix[-1] causes undefined [OK]
Hint: Initialize prefix[0] before loop to avoid undefined [OK]
Common Mistakes:
Starting loop at 0 without initializing prefix[0]
Ignoring undefined prefix[-1] access
Assuming prefix[-1] is zero automatically
5. Given an array arr = [3, 1, 4, 1, 5], how can you use a prefix sum array to efficiently find the sum of elements between indices 1 and 3 (inclusive)?
hard
A. Calculate prefix sums, then subtract prefix[0] from prefix[4].
B. Calculate prefix sums, then subtract prefix[0] from prefix[3].
C. Calculate prefix sums, then subtract prefix[0] from prefix[2].
D. Calculate prefix sums, then subtract prefix[1] from prefix[3].
Solution
Step 1: Build prefix sum array for arr
prefix = [3, 4, 8, 9, 14] where prefix[i] = sum of arr[0..i].