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?
Given the following TypeScript code that converts a string to an integer, what will be printed when calling
myAtoi(" -42abc")?DSA Typescript
function myAtoi(s: string): number {
let 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 && s[i] >= '0' && s[i] <= '9') {
result = result * 10 + (s.charCodeAt(i) - '0'.charCodeAt(0));
i++;
}
return sign * result;
}
console.log(myAtoi(" -42abc"));Attempts:
2 left
💡 Hint
Focus on how the function skips spaces, checks sign, and parses digits until a non-digit.
✗ Incorrect
The function skips leading spaces, detects the '-' sign, then parses digits '4' and '2' until it hits 'a' which is not a digit. So it returns -42.
❓ Predict Output
intermediateWhat does this atoi variant return for input "4193 with words"?
Consider this TypeScript function that converts a string to an integer. What is the output of
myAtoi("4193 with words")?DSA Typescript
function myAtoi(s: string): number {
let 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 && s[i] >= '0' && s[i] <= '9') {
result = result * 10 + (s.charCodeAt(i) - '0'.charCodeAt(0));
i++;
}
return sign * result;
}
console.log(myAtoi("4193 with words"));Attempts:
2 left
💡 Hint
The function stops parsing digits when it encounters a non-digit character.
✗ Incorrect
The function parses digits '4', '1', '9', '3' and stops at the space before 'with'. It returns 4193.
🔧 Debug
advancedWhat is the output of this atoi code for "+-12"?
This TypeScript function attempts to convert a string to an integer. What is the output of
myAtoi("+-12")?DSA Typescript
function myAtoi(s: string): number {
let 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++;
}
if (i < s.length && (s[i] === '+' || s[i] === '-')) {
sign = s[i] === '-' ? -1 : 1;
i++;
}
while (i < s.length && s[i] >= '0' && s[i] <= '9') {
result = result * 10 + (s.charCodeAt(i) - '0'.charCodeAt(0));
i++;
}
return sign * result;
}
console.log(myAtoi("+-12"));Attempts:
2 left
💡 Hint
Check how the function handles multiple signs in a row.
✗ Incorrect
The function reads the first '+' as sign +1, then reads the second '-' as sign -1, then parses digits '1' and '2' for result=12. Returns -12.
🚀 Application
advancedHow many digits are parsed by this atoi call?
Using this TypeScript atoi function, how many digits are parsed when calling
myAtoi("0000012345abc")?DSA Typescript
function myAtoi(s: string): number {
let 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++;
}
let count = 0;
while (i < s.length && s[i] >= '0' && s[i] <= '9') {
result = result * 10 + (s.charCodeAt(i) - '0'.charCodeAt(0));
i++;
count++;
}
console.log(count);
return sign * result;
}
myAtoi("0000012345abc");Attempts:
2 left
💡 Hint
Count all digits including leading zeros until first non-digit.
✗ Incorrect
The function counts all digits including leading zeros: '0','0','0','0','0','1','2','3','4','5' = 10 digits.
🧠 Conceptual
expertWhat is the output of this atoi with overflow handling?
This TypeScript function converts a string to a 32-bit signed integer with overflow checks. What is the output of
myAtoi("9223372036854775808")?DSA Typescript
function myAtoi(s: string): number {
let i = 0, sign = 1, result = 0;
const INT_MAX = 2147483647;
const INT_MIN = -2147483648;
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 && s[i] >= '0' && s[i] <= '9') {
const digit = s.charCodeAt(i) - '0'.charCodeAt(0);
if (result > Math.floor(INT_MAX / 10) || (result === Math.floor(INT_MAX / 10) && digit > INT_MAX % 10)) {
return sign === 1 ? INT_MAX : INT_MIN;
}
result = result * 10 + digit;
i++;
}
return sign * result;
}
console.log(myAtoi("9223372036854775808"));Attempts:
2 left
💡 Hint
Check how the function limits the result to 32-bit integer max/min values.
✗ Incorrect
The input number is larger than 32-bit max, so the function returns INT_MAX = 2147483647.
