Challenge - 5 Problems
Atoi Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this Rust atoi implementation?
Consider the following Rust function that converts a string to an integer. What will be printed when calling
my_atoi(" -42abc")?DSA Rust
fn my_atoi(s: &str) -> i32 { let s = s.trim_start(); let mut chars = s.chars().peekable(); let mut sign = 1; if let Some(&c) = chars.peek() { if c == '-' { sign = -1; chars.next(); } else if c == '+' { chars.next(); } } let mut result: i32 = 0; while let Some(c) = chars.next() { if let Some(d) = c.to_digit(10) { match result.checked_mul(10).and_then(|r| r.checked_add(d as i32)) { Some(val) => result = val, None => return if sign == 1 { i32::MAX } else { i32::MIN }, } } else { break; } } result * sign } fn main() { println!("{}", my_atoi(" -42abc")); }
Attempts:
2 left
💡 Hint
Focus on how the function handles leading spaces, signs, and stops parsing at the first non-digit character.
✗ Incorrect
The function trims leading spaces, detects the negative sign, then parses digits until a non-digit character is found. It stops at 'a' and returns -42.
❓ Predict Output
intermediateWhat is the output when input string is empty?
Given the same
my_atoi function, what will be the output of my_atoi("")?DSA Rust
fn my_atoi(s: &str) -> i32 { let s = s.trim_start(); let mut chars = s.chars().peekable(); let mut sign = 1; if let Some(&c) = chars.peek() { if c == '-' { sign = -1; chars.next(); } else if c == '+' { chars.next(); } } let mut result: i32 = 0; while let Some(c) = chars.next() { if let Some(d) = c.to_digit(10) { match result.checked_mul(10).and_then(|r| r.checked_add(d as i32)) { Some(val) => result = val, None => return if sign == 1 { i32::MAX } else { i32::MIN }, } } else { break; } } result * sign } fn main() { println!("{}", my_atoi("")); }
Attempts:
2 left
💡 Hint
Think about what happens when the input string has no characters.
✗ Incorrect
The function trims spaces and finds no characters to parse, so it returns 0.
❓ Predict Output
advancedWhat is the output when input causes integer overflow?
What will
my_atoi("9223372036854775808") return? This input is larger than i32 max value.DSA Rust
fn my_atoi(s: &str) -> i32 { let s = s.trim_start(); let mut chars = s.chars().peekable(); let mut sign = 1; if let Some(&c) = chars.peek() { if c == '-' { sign = -1; chars.next(); } else if c == '+' { chars.next(); } } let mut result: i32 = 0; while let Some(c) = chars.next() { if let Some(d) = c.to_digit(10) { match result.checked_mul(10).and_then(|r| r.checked_add(d as i32)) { Some(val) => result = val, None => return if sign == 1 { i32::MAX } else { i32::MIN }, } } else { break; } } result * sign } fn main() { println!("{}", my_atoi("9223372036854775808")); }
Attempts:
2 left
💡 Hint
Check how the function handles overflow using checked arithmetic.
✗ Incorrect
The function detects overflow and returns i32::MAX (2147483647) for positive overflow.
❓ Predict Output
advancedWhat is the output when input has only sign and no digits?
What will
my_atoi("+") return?DSA Rust
fn my_atoi(s: &str) -> i32 { let s = s.trim_start(); let mut chars = s.chars().peekable(); let mut sign = 1; if let Some(&c) = chars.peek() { if c == '-' { sign = -1; chars.next(); } else if c == '+' { chars.next(); } } let mut result: i32 = 0; while let Some(c) = chars.next() { if let Some(d) = c.to_digit(10) { match result.checked_mul(10).and_then(|r| r.checked_add(d as i32)) { Some(val) => result = val, None => return if sign == 1 { i32::MAX } else { i32::MIN }, } } else { break; } } result * sign } fn main() { println!("{}", my_atoi("+")); }
Attempts:
2 left
💡 Hint
Consider what happens if no digits follow the sign.
✗ Incorrect
No digits are parsed after the sign, so the function returns 0.
🧠 Conceptual
expertWhy does the function use checked_mul and checked_add?
In the atoi implementation, why are
checked_mul and checked_add used instead of normal multiplication and addition?Attempts:
2 left
💡 Hint
Think about what happens when the number is too big to fit in i32.
✗ Incorrect
checked_mul and checked_add return None if overflow occurs, allowing the function to safely return i32 limits.
