Challenge - 5 Problems
Atoi Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this atoi implementation for input " -42abc"?
Consider the following C# code that converts a string to an integer similar to atoi. What will be printed?
DSA C#
using System; class Program { static int MyAtoi(string s) { int i = 0, sign = 1, result = 0; while (i < s.Length && s[i] == ' ') i++; if (i < s.Length && (s[i] == '+' || s[i] == '-')) { sign = s[i] == '-' ? -1 : 1; i++; } while (i < s.Length && Char.IsDigit(s[i])) { int digit = s[i] - '0'; if (result > (Int32.MaxValue - digit) / 10) { return sign == 1 ? Int32.MaxValue : Int32.MinValue; } result = result * 10 + digit; i++; } return sign * result; } static void Main() { Console.WriteLine(MyAtoi(" -42abc")); } }
Attempts:
2 left
💡 Hint
Focus on how the code skips spaces, handles sign, and stops reading digits at the first non-digit.
✗ Incorrect
The code skips leading spaces, detects the '-' sign, then reads digits '4' and '2'. It stops at 'a' which is not a digit. So the result is -42.
❓ Predict Output
intermediateWhat does this atoi code return for input "4193 with words"?
Analyze the following C# atoi-like function and determine the output when input is "4193 with words".
DSA C#
using System; class Program { static int MyAtoi(string s) { int i = 0, sign = 1, result = 0; while (i < s.Length && s[i] == ' ') i++; if (i < s.Length && (s[i] == '+' || s[i] == '-')) { sign = s[i] == '-' ? -1 : 1; i++; } while (i < s.Length && Char.IsDigit(s[i])) { int digit = s[i] - '0'; if (result > (Int32.MaxValue - digit) / 10) { return sign == 1 ? Int32.MaxValue : Int32.MinValue; } result = result * 10 + digit; i++; } return sign * result; } static void Main() { Console.WriteLine(MyAtoi("4193 with words")); } }
Attempts:
2 left
💡 Hint
The function reads digits until a non-digit character is found.
✗ Incorrect
The function reads '4', '1', '9', '3' then stops at the space before 'with'. So it returns 4193.
❓ Predict Output
advancedWhat is the output when input is "-91283472332" (overflow case)?
Given this atoi implementation, what will be the output for input string "-91283472332"?
DSA C#
using System; class Program { static int MyAtoi(string s) { int i = 0, sign = 1, result = 0; while (i < s.Length && s[i] == ' ') i++; if (i < s.Length && (s[i] == '+' || s[i] == '-')) { sign = s[i] == '-' ? -1 : 1; i++; } while (i < s.Length && Char.IsDigit(s[i])) { int digit = s[i] - '0'; if (result > (Int32.MaxValue - digit) / 10) { return sign == 1 ? Int32.MaxValue : Int32.MinValue; } result = result * 10 + digit; i++; } return sign * result; } static void Main() { Console.WriteLine(MyAtoi("-91283472332")); } }
Attempts:
2 left
💡 Hint
Check how the code handles integer overflow for negative numbers.
✗ Incorrect
The number is less than Int32.MinValue, so the function returns Int32.MinValue (-2147483648).
❓ Predict Output
advancedWhat is the output for input "+-12"?
Analyze the atoi code below. What will it output for the input string "+-12"?
DSA C#
using System; class Program { static int MyAtoi(string s) { int i = 0, sign = 1, result = 0; while (i < s.Length && s[i] == ' ') i++; if (i < s.Length && (s[i] == '+' || s[i] == '-')) { sign = s[i] == '-' ? -1 : 1; i++; } while (i < s.Length && Char.IsDigit(s[i])) { int digit = s[i] - '0'; if (result > (Int32.MaxValue - digit) / 10) { return sign == 1 ? Int32.MaxValue : Int32.MinValue; } result = result * 10 + digit; i++; } return sign * result; } static void Main() { Console.WriteLine(MyAtoi("+-12")); } }
Attempts:
2 left
💡 Hint
The code only accepts one optional sign character before digits.
✗ Incorrect
The code reads '+' as sign, then sees '-' which is not a digit, so it stops and returns 0.
🧠 Conceptual
expertWhy does the atoi implementation check overflow with condition 'result > (Int32.MaxValue - digit) / 10'?
In the atoi code, before adding a new digit, it checks if 'result > (Int32.MaxValue - digit) / 10'. Why is this check necessary?
Attempts:
2 left
💡 Hint
Think about what happens if you multiply and add digits beyond the max integer limit.
✗ Incorrect
Multiplying result by 10 and adding digit can exceed Int32.MaxValue, causing overflow. The check prevents this by returning max/min values.
