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 atoi conversion for input with leading spaces and sign?
Consider the input string " -42". What integer value does the atoi function return after processing this string?
DSA Go
func myAtoi(s string) int { i, sign, result := 0, 1, 0 n := len(s) for i < n && s[i] == ' ' { i++ } if i < n && (s[i] == '+' || s[i] == '-') { if s[i] == '-' { sign = -1 } i++ } for i < n && s[i] >= '0' && s[i] <= '9' { result = result*10 + int(s[i]-'0') i++ } return sign * result }
Attempts:
2 left
💡 Hint
Remember to skip spaces and check the sign before converting digits.
✗ Incorrect
The function skips leading spaces, detects the '-' sign, then converts digits until a non-digit is found. So the output is -42.
❓ Predict Output
intermediateWhat is the output when input string contains non-digit characters after number?
Given the input string "4193 with words", what integer does the atoi function return?
DSA Go
func myAtoi(s string) int { i, sign, result := 0, 1, 0 n := len(s) for i < n && s[i] == ' ' { i++ } if i < n && (s[i] == '+' || s[i] == '-') { if s[i] == '-' { sign = -1 } i++ } for i < n && s[i] >= '0' && s[i] <= '9' { result = result*10 + int(s[i]-'0') i++ } return sign * result }
Attempts:
2 left
💡 Hint
Conversion stops at the first non-digit character after digits.
✗ Incorrect
The function converts digits until it encounters a non-digit character, so it returns 4193.
❓ Predict Output
advancedWhat is the output when input string represents a number exceeding 32-bit integer max?
Given the input string "91283472332", what integer does the atoi function return considering 32-bit signed integer limits?
DSA Go
func myAtoi(s string) int { i, sign, result := 0, 1, 0 n := len(s) INT_MAX, INT_MIN := 2147483647, -2147483648 for i < n && s[i] == ' ' { i++ } if i < n && (s[i] == '+' || s[i] == '-') { if s[i] == '-' { sign = -1 } i++ } for i < n && s[i] >= '0' && s[i] <= '9' { digit := int(s[i] - '0') if result > (INT_MAX-digit)/10 { if sign == 1 { return INT_MAX } else { return INT_MIN } } result = result*10 + digit i++ } return sign * result }
Attempts:
2 left
💡 Hint
Check for overflow and clamp to 32-bit integer limits.
✗ Incorrect
The number exceeds 32-bit max, so the function returns INT_MAX (2147483647).
❓ Predict Output
advancedWhat is the output when input string has only non-digit characters?
Given the input string "words and 987", what integer does the atoi function return?
DSA Go
func myAtoi(s string) int { i, sign, result := 0, 1, 0 n := len(s) for i < n && s[i] == ' ' { i++ } if i < n && (s[i] == '+' || s[i] == '-') { if s[i] == '-' { sign = -1 } i++ } for i < n && s[i] >= '0' && s[i] <= '9' { result = result*10 + int(s[i]-'0') i++ } return sign * result }
Attempts:
2 left
💡 Hint
If the first non-space character is not a digit or sign, return 0.
✗ Incorrect
The function finds no digits at the start, so it returns 0.
🧠 Conceptual
expertWhat is the main reason to check overflow before multiplying result by 10 in atoi implementation?
In the atoi function, why do we check if result > (INT_MAX - digit) / 10 before updating result = result * 10 + digit?
Attempts:
2 left
💡 Hint
Think about what happens if the number becomes too large for the integer type.
✗ Incorrect
Checking overflow before multiplication prevents the integer from wrapping around and producing wrong values.
