Challenge - 5 Problems
Subarray Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Subarray Sum Count Function
What is the output of the following Rust function that counts subarrays summing to k?
DSA Rust
fn subarray_sum(nums: Vec<i32>, k: i32) -> i32 {
use std::collections::HashMap;
let mut count = 0;
let mut sum = 0;
let mut map = HashMap::new();
map.insert(0, 1);
for num in nums {
sum += num;
if let Some(&c) = map.get(&(sum - k)) {
count += c;
}
*map.entry(sum).or_insert(0) += 1;
}
count
}
fn main() {
let nums = vec![1, 2, 3];
let k = 3;
println!("{}", subarray_sum(nums, k));
}Attempts:
2 left
💡 Hint
Think about all continuous subarrays that sum to 3.
✗ Incorrect
The subarrays [1, 2] and [3] both sum to 3, so the count is 2.
🧠 Conceptual
intermediateUnderstanding Hash Map Role in Subarray Sum
In the subarray sum equals k problem, what is the main purpose of the hash map?
Attempts:
2 left
💡 Hint
Think about how prefix sums help find subarrays quickly.
✗ Incorrect
The hash map stores how many times each prefix sum has appeared, allowing quick lookup to find if a subarray summing to k exists ending at current index.
🔧 Debug
advancedIdentify the Error in Subarray Sum Code
What error does the following Rust code produce when run?
DSA Rust
fn subarray_sum(nums: Vec<i32>, k: i32) -> i32 {
use std::collections::HashMap;
let mut count = 0;
let mut sum = 0;
let mut map = HashMap::new();
map.insert(0, 1);
for num in nums {
sum += num;
if let Some(c) = map.get(&(sum - k)) {
count += *c;
}
map.insert(sum, map[&sum] + 1);
}
count
}
fn main() {
let nums = vec![1, 2, 3];
let k = 3;
println!("{}", subarray_sum(nums, k));
}Attempts:
2 left
💡 Hint
Check how map[&sum] is accessed before insertion.
✗ Incorrect
The code tries to access map[&sum] before it is inserted, causing a panic at runtime due to missing key.
❓ Predict Output
advancedOutput of Subarray Sum with Negative Numbers
What is the output of this Rust code counting subarrays summing to k with negative numbers?
DSA Rust
fn subarray_sum(nums: Vec<i32>, k: i32) -> i32 {
use std::collections::HashMap;
let mut count = 0;
let mut sum = 0;
let mut map = HashMap::new();
map.insert(0, 1);
for num in nums {
sum += num;
if let Some(&c) = map.get(&(sum - k)) {
count += c;
}
*map.entry(sum).or_insert(0) += 1;
}
count
}
fn main() {
let nums = vec![1, -1, 0];
let k = 0;
println!("{}", subarray_sum(nums, k));
}Attempts:
2 left
💡 Hint
Consider all subarrays that sum to zero, including single elements.
✗ Incorrect
The subarrays [1, -1], [1, -1, 0], and [0] all sum to 0, so the count is 3.
🚀 Application
expertNumber of Subarrays with Sum Equal to K in Large Input
Given an array of 100,000 elements all equal to 1 and k = 50, what is the number of subarrays that sum to k?
Attempts:
2 left
💡 Hint
Think about how many continuous subarrays of length 50 exist in an array of length 100,000.
✗ Incorrect
Each subarray of length 50 sums to 50 since all elements are 1. The number of such subarrays is (100000 - 50 + 1) = 99951.
