Challenge - 5 Problems
Atoi Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this atoi implementation with input " -42abc"?
Consider the following Java method that converts a string to an integer similar to atoi. What will be printed when calling
myAtoi(" -42abc")?DSA Java
public class Solution { public static int myAtoi(String s) { int i = 0, sign = 1, result = 0; int n = s.length(); while (i < n && s.charAt(i) == ' ') i++; if (i < n && (s.charAt(i) == '+' || s.charAt(i) == '-')) { sign = (s.charAt(i) == '-') ? -1 : 1; i++; } while (i < n && Character.isDigit(s.charAt(i))) { int digit = s.charAt(i) - '0'; if (result > (Integer.MAX_VALUE - digit) / 10) { return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE; } result = result * 10 + digit; i++; } return result * sign; } public static void main(String[] args) { System.out.println(myAtoi(" -42abc")); } }
Attempts:
2 left
💡 Hint
Focus on how the method skips spaces, reads the sign, and stops reading digits at the first non-digit character.
✗ Incorrect
The method skips leading spaces, reads 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 variant return for input "4193 with words"?
Given this Java method similar to atoi, what will be the output when calling
myAtoi("4193 with words")?DSA Java
public class Solution { public static int myAtoi(String s) { int i = 0, sign = 1, result = 0; int n = s.length(); while (i < n && s.charAt(i) == ' ') i++; if (i < n && (s.charAt(i) == '+' || s.charAt(i) == '-')) { sign = (s.charAt(i) == '-') ? -1 : 1; i++; } while (i < n && Character.isDigit(s.charAt(i))) { int digit = s.charAt(i) - '0'; if (result > (Integer.MAX_VALUE - digit) / 10) { return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE; } result = result * 10 + digit; i++; } return result * sign; } public static void main(String[] args) { System.out.println(myAtoi("4193 with words")); } }
Attempts:
2 left
💡 Hint
The method stops reading digits once it encounters a non-digit character.
✗ Incorrect
The method reads digits '4', '1', '9', '3' and stops at the space after '3'. So it returns 4193.
❓ Predict Output
advancedWhat is the output when input is "-91283472332" causing overflow?
What will the following Java atoi-like method return when called with
myAtoi("-91283472332")?DSA Java
public class Solution { public static int myAtoi(String s) { int i = 0, sign = 1, result = 0; int n = s.length(); while (i < n && s.charAt(i) == ' ') i++; if (i < n && (s.charAt(i) == '+' || s.charAt(i) == '-')) { sign = (s.charAt(i) == '-') ? -1 : 1; i++; } while (i < n && Character.isDigit(s.charAt(i))) { int digit = s.charAt(i) - '0'; if (result > (Integer.MAX_VALUE - digit) / 10) { return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE; } result = result * 10 + digit; i++; } return result * sign; } public static void main(String[] args) { System.out.println(myAtoi("-91283472332")); } }
Attempts:
2 left
💡 Hint
Check how the method handles overflow by returning min or max integer values.
✗ Incorrect
The number is less than Integer.MIN_VALUE, so the method returns Integer.MIN_VALUE (-2147483648).
❓ Predict Output
advancedWhat is the output for input "+-12" in this atoi implementation?
What will the following Java method print when called with
myAtoi("+-12")?DSA Java
public class Solution { public static int myAtoi(String s) { int i = 0, sign = 1, result = 0; int n = s.length(); while (i < n && s.charAt(i) == ' ') i++; if (i < n && (s.charAt(i) == '+' || s.charAt(i) == '-')) { sign = (s.charAt(i) == '-') ? -1 : 1; i++; } while (i < n && Character.isDigit(s.charAt(i))) { int digit = s.charAt(i) - '0'; if (result > (Integer.MAX_VALUE - digit) / 10) { return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE; } result = result * 10 + digit; i++; } return result * sign; } public static void main(String[] args) { System.out.println(myAtoi("+-12")); } }
Attempts:
2 left
💡 Hint
The method only accepts one optional sign character before digits.
✗ Incorrect
The method reads '+' as sign, then sees '-' which is not a digit, so it stops parsing and returns 0.
🧠 Conceptual
expertWhich step is crucial to prevent integer overflow in atoi implementation?
In an atoi implementation converting string to 32-bit signed integer, which step is essential to avoid integer overflow during digit accumulation?
Attempts:
2 left
💡 Hint
Think about how to detect if adding a digit will exceed the integer limit before it happens.
✗ Incorrect
Checking if result > (Integer.MAX_VALUE - digit) / 10 ensures no overflow occurs when multiplying by 10 and adding digit.
