Challenge - 5 Problems
Two Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this Rust code for Two Sum?
Given the Rust function below that finds two indices of numbers adding up to the target, what will be printed when calling
two_sum(vec![2, 7, 11, 15], 9)?DSA Rust
use std::collections::HashMap;
fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
let mut map = HashMap::new();
for (i, &num) in nums.iter().enumerate() {
let complement = target - num;
if let Some(&index) = map.get(&complement) {
return vec![index as i32, i as i32];
}
map.insert(num, i);
}
vec![]
}
fn main() {
let result = two_sum(vec![2, 7, 11, 15], 9);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Think about which two numbers add up to 9 and their indices in the vector.
✗ Incorrect
The numbers 2 and 7 add up to 9. Their indices are 0 and 1 respectively, so the function returns [0, 1].
❓ Predict Output
intermediateWhat does this Rust code print for no solution case?
What will be the output of the Rust program below when calling
two_sum(vec![1, 2, 3], 7)?DSA Rust
use std::collections::HashMap;
fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
let mut map = HashMap::new();
for (i, &num) in nums.iter().enumerate() {
let complement = target - num;
if let Some(&index) = map.get(&complement) {
return vec![index as i32, i as i32];
}
map.insert(num, i);
}
vec![]
}
fn main() {
let result = two_sum(vec![1, 2, 3], 7);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Check if any two numbers add up to 7 in the list.
✗ Incorrect
No two numbers in [1, 2, 3] add up to 7, so the function returns an empty vector [].
🔧 Debug
advancedWhat error does this Rust code cause?
Consider this Rust code snippet for Two Sum. What error will it produce when compiled?
DSA Rust
use std::collections::HashMap;
fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
let mut map = HashMap::new();
for (i, &num) in nums.iter().enumerate() {
let complement = target - num;
if let Some(&index) = map.get(&complement) {
return vec![index as i32, i as i32];
}
map.insert(num, i);
}
vec![]
}Attempts:
2 left
💡 Hint
Look carefully at the line where HashMap is created.
✗ Incorrect
The line 'let mut map = HashMap::new()' is missing a semicolon at the end, causing a syntax error.
❓ Predict Output
advancedWhat is the output of this Rust code with duplicate numbers?
What will be printed when running this Rust program?
DSA Rust
use std::collections::HashMap;
fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
let mut map = HashMap::new();
for (i, &num) in nums.iter().enumerate() {
let complement = target - num;
if let Some(&index) = map.get(&complement) {
return vec![index as i32, i as i32];
}
map.insert(num, i);
}
vec![]
}
fn main() {
let result = two_sum(vec![3, 3, 4, 5], 6);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Check which two numbers add up to 6 and their indices.
✗ Incorrect
The two 3's at indices 0 and 1 add up to 6, so the function returns [0, 1].
🧠 Conceptual
expertWhy does the Two Sum hash solution run in O(n) time?
Which reason best explains why the classic Two Sum hash map solution runs in linear time?
Attempts:
2 left
💡 Hint
Think about how hash maps help with quick lookups.
✗ Incorrect
The hash map allows checking if the complement exists in constant time, so the entire array is processed once, resulting in O(n) time.
