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?
Given the following C++ code that converts a string to an integer, what will be printed?
DSA C++
int myAtoi(string s) { int i = 0, sign = 1, result = 0; while (i < s.size() && s[i] == ' ') i++; if (i < s.size() && (s[i] == '+' || s[i] == '-')) { sign = (s[i] == '-') ? -1 : 1; i++; } while (i < s.size() && isdigit(s[i])) { int digit = s[i] - '0'; if (result > (INT_MAX - digit) / 10) { return sign == 1 ? INT_MAX : INT_MIN; } result = result * 10 + digit; i++; } return sign * result; } int main() { cout << myAtoi(" -42abc") << endl; return 0; }
Attempts:
2 left
💡 Hint
Focus on how the function skips spaces and handles the sign before digits.
✗ Incorrect
The function skips leading spaces, detects the '-' sign, then reads digits until a non-digit character. It converts "-42" correctly and stops at 'a'.
❓ Predict Output
intermediateWhat does this atoi variant output?
Consider this C++ code snippet. What will it print?
DSA C++
int myAtoi(string s) { int i = 0, sign = 1, result = 0; while (i < s.size() && s[i] == ' ') i++; if (i < s.size() && (s[i] == '+' || s[i] == '-')) { sign = (s[i] == '-') ? -1 : 1; i++; } while (i < s.size() && isdigit(s[i])) { int digit = s[i] - '0'; if (result > (INT_MAX - digit) / 10) { return sign == 1 ? INT_MAX : INT_MIN; } result = result * 10 + digit; i++; } return sign * result; } int main() { cout << myAtoi("4193 with words") << endl; return 0; }
Attempts:
2 left
💡 Hint
Check how digits are read until a non-digit character.
✗ Incorrect
The function reads digits '4193' and stops at the space before 'with'. It returns 4193.
❓ Predict Output
advancedWhat is the output when input causes overflow?
What will this code print when converting a string representing a number larger than INT_MAX?
DSA C++
int myAtoi(string s) { int i = 0, sign = 1, result = 0; while (i < s.size() && s[i] == ' ') i++; if (i < s.size() && (s[i] == '+' || s[i] == '-')) { sign = (s[i] == '-') ? -1 : 1; i++; } while (i < s.size() && isdigit(s[i])) { int digit = s[i] - '0'; if (result > (INT_MAX - digit) / 10) { return sign == 1 ? INT_MAX : INT_MIN; } result = result * 10 + digit; i++; } return sign * result; } int main() { cout << myAtoi("9223372036854775808") << endl; return 0; }
Attempts:
2 left
💡 Hint
Check how the function handles overflow by comparing with INT_MAX.
✗ Incorrect
The input exceeds INT_MAX, so the function returns INT_MAX (2147483647) to avoid overflow.
❓ Predict Output
advancedWhat is the output when input has no digits?
What will this code print when the input string has no digits after optional spaces and sign?
DSA C++
int myAtoi(string s) { int i = 0, sign = 1, result = 0; while (i < s.size() && s[i] == ' ') i++; if (i < s.size() && (s[i] == '+' || s[i] == '-')) { sign = (s[i] == '-') ? -1 : 1; i++; } while (i < s.size() && isdigit(s[i])) { int digit = s[i] - '0'; if (result > (INT_MAX - digit) / 10) { return sign == 1 ? INT_MAX : INT_MIN; } result = result * 10 + digit; i++; } return sign * result; } int main() { cout << myAtoi(" +abc") << endl; return 0; }
Attempts:
2 left
💡 Hint
If no digits are found after optional sign, what does the function return?
✗ Incorrect
No digits found, so result remains 0 and function returns 0.
🧠 Conceptual
expertWhich step is crucial to prevent integer overflow in atoi?
In the atoi implementation, which step ensures the integer does not overflow when converting large strings?
Attempts:
2 left
💡 Hint
Think about how to detect if adding a new digit will exceed the integer limit.
✗ Incorrect
The check before adding a digit prevents overflow by ensuring the next multiplication and addition won't exceed INT_MAX.
