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 JavaScript function that converts a string to an integer similar to atoi. What will be printed when calling
myAtoi(" -42abc")?DSA Javascript
function myAtoi(s) {
let i = 0, sign = 1, result = 0;
while (s[i] === ' ') i++;
if (s[i] === '-' || s[i] === '+') {
sign = s[i] === '-' ? -1 : 1;
i++;
}
while (i < s.length && s[i] >= '0' && s[i] <= '9') {
result = result * 10 + (s[i].charCodeAt(0) - '0'.charCodeAt(0));
i++;
}
return sign * result;
}
console.log(myAtoi(" -42abc"));Attempts:
2 left
💡 Hint
Focus on how the function skips spaces, reads the sign, and stops reading digits when a non-digit appears.
✗ Incorrect
The function skips leading spaces, detects the '-' sign, then reads digits '4' and '2' before stopping at 'a'. It returns -42.
❓ Predict Output
intermediateWhat is the output of this atoi variant for input "+4193 with words"?
Given this JavaScript function similar to atoi, what will
myAtoi("+4193 with words") print?DSA Javascript
function myAtoi(s) {
let i = 0, sign = 1, result = 0;
while (s[i] === ' ') i++;
if (s[i] === '-' || s[i] === '+') {
sign = s[i] === '-' ? -1 : 1;
i++;
}
while (i < s.length && s[i] >= '0' && s[i] <= '9') {
result = result * 10 + (s[i].charCodeAt(0) - '0'.charCodeAt(0));
i++;
}
return sign * result;
}
console.log(myAtoi("+4193 with words"));Attempts:
2 left
💡 Hint
Check how the function handles the '+' sign and stops reading digits at the first non-digit.
✗ Incorrect
The function reads the '+' sign as positive, then parses digits '4', '1', '9', '3' before stopping at space.
❓ Predict Output
advancedWhat is the output of this atoi function for input "words and 987"?
What will the following JavaScript function print when called with
myAtoi("words and 987")?DSA Javascript
function myAtoi(s) {
let i = 0, sign = 1, result = 0;
while (s[i] === ' ') i++;
if (s[i] === '-' || s[i] === '+') {
sign = s[i] === '-' ? -1 : 1;
i++;
}
if (s[i] < '0' || s[i] > '9') return 0;
while (i < s.length && s[i] >= '0' && s[i] <= '9') {
result = result * 10 + (s[i].charCodeAt(0) - '0'.charCodeAt(0));
i++;
}
return sign * result;
}
console.log(myAtoi("words and 987"));Attempts:
2 left
💡 Hint
Notice the check after skipping spaces and sign that returns 0 if the next character is not a digit.
✗ Incorrect
Since the first non-space character is 'w' which is not a digit or sign, the function returns 0 immediately.
❓ Predict Output
advancedWhat is the output of this atoi function for input "-91283472332" considering 32-bit integer limits?
Given this JavaScript atoi function that clamps output to 32-bit signed integer range, what will
myAtoi("-91283472332") print?DSA Javascript
function myAtoi(s) {
const INT_MAX = 2147483647;
const INT_MIN = -2147483648;
let i = 0, sign = 1, result = 0;
while (s[i] === ' ') i++;
if (s[i] === '-' || s[i] === '+') {
sign = s[i] === '-' ? -1 : 1;
i++;
}
while (i < s.length && s[i] >= '0' && s[i] <= '9') {
result = result * 10 + (s[i].charCodeAt(0) - '0'.charCodeAt(0));
i++;
if (sign * result < INT_MIN) return INT_MIN;
if (sign * result > INT_MAX) return INT_MAX;
}
return sign * result;
}
console.log(myAtoi("-91283472332"));Attempts:
2 left
💡 Hint
Check how the function clamps the result to INT_MIN and INT_MAX during parsing.
✗ Incorrect
The number is less than INT_MIN, so the function returns INT_MIN (-2147483648) to clamp the value.
❓ Predict Output
expertWhat is the output of this atoi function for input " +0000000000012345678"?
Analyze this JavaScript atoi function and determine what
myAtoi(" +0000000000012345678") prints.DSA Javascript
function myAtoi(s) {
let i = 0, sign = 1, result = 0;
while (s[i] === ' ') i++;
if (s[i] === '-' || s[i] === '+') {
sign = s[i] === '-' ? -1 : 1;
i++;
}
while (i < s.length && s[i] >= '0' && s[i] <= '9') {
result = result * 10 + (s[i].charCodeAt(0) - '0'.charCodeAt(0));
i++;
}
return sign * result;
}
console.log(myAtoi(" +0000000000012345678"));Attempts:
2 left
💡 Hint
Leading zeros do not affect the numeric value; the function accumulates digits normally.
✗ Incorrect
The function skips spaces and '+' sign, then reads digits including leading zeros, resulting in 12345678.
