Challenge - 5 Problems
Prefix Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Prefix Sum Array Construction
What is the output of the following Rust code that builds a prefix sum array from the input array?
DSA Rust
fn main() {
let arr = vec![3, 1, 4, 1, 5];
let mut prefix_sum = vec![0; arr.len() + 1];
for i in 0..arr.len() {
prefix_sum[i + 1] = prefix_sum[i] + arr[i];
}
println!("{:?}", prefix_sum);
}Attempts:
2 left
💡 Hint
Remember the prefix sum array starts with 0 and each next element is the sum of all previous elements in the input array.
✗ Incorrect
The prefix sum array starts with 0. Each element at index i+1 is the sum of all elements from index 0 to i in the original array. So for arr = [3,1,4,1,5], prefix_sum = [0,3,4,8,9,14].
🧠 Conceptual
intermediatePurpose of Prefix Sum Array
What is the main advantage of using a prefix sum array when working with an array of numbers?
Attempts:
2 left
💡 Hint
Think about how prefix sums help with repeated sum queries on subarrays.
✗ Incorrect
Prefix sum arrays let you find the sum of any subarray quickly by subtracting two prefix sums, avoiding repeated addition.
🔧 Debug
advancedIdentify the Error in Prefix Sum Calculation
What error does the following Rust code produce when trying to build a prefix sum array?
DSA Rust
fn main() {
let arr = vec![2, 4, 6];
let mut prefix_sum = vec![0; arr.len()];
for i in 1..arr.len() {
prefix_sum[i] = prefix_sum[i - 1] + arr[i];
}
println!("{:?}", prefix_sum);
}Attempts:
2 left
💡 Hint
Check how prefix_sum is initialized and how the loop starts.
✗ Incorrect
The prefix_sum vector has length equal to arr, so prefix_sum[0] is 0 and never updated. The loop starts at i=1, so prefix_sum[1] = prefix_sum[0] + arr[1] = 0 + 4 = 4, but prefix_sum[0] should be arr[0]. This causes incorrect prefix sums.
❓ Predict Output
advancedOutput After Using Prefix Sum to Query Subarray Sum
Given the prefix sum array, what is the output of the code that calculates the sum of elements from index 1 to 3 (inclusive) in the original array?
DSA Rust
fn main() {
let arr = vec![1, 2, 3, 4, 5];
let mut prefix_sum = vec![0; arr.len() + 1];
for i in 0..arr.len() {
prefix_sum[i + 1] = prefix_sum[i] + arr[i];
}
let start = 1;
let end = 3;
let sub_sum = prefix_sum[end + 1] - prefix_sum[start];
println!("{}", sub_sum);
}Attempts:
2 left
💡 Hint
Sum from index 1 to 3 means sum of elements 2, 3, and 4.
✗ Incorrect
The sum is arr[1] + arr[2] + arr[3] = 2 + 3 + 4 = 9. Using prefix sums, sub_sum = prefix_sum[4] - prefix_sum[1] = 10 - 1 = 9.
🧠 Conceptual
expertTime Complexity of Multiple Range Sum Queries Using Prefix Sum
If you have an array of size n and you want to answer m queries asking for the sum of elements between indices i and j, what is the overall time complexity using a prefix sum array?
Attempts:
2 left
💡 Hint
Building prefix sums takes linear time, each query is constant time.
✗ Incorrect
Building the prefix sum array takes O(n). Each query can be answered in O(1) by subtracting two prefix sums. So total is O(n + m).
