It converts a string representing a number into its integer form, ignoring leading spaces and handling optional signs (+ or -).
Click to reveal answer
beginner
How does atoi handle leading spaces in the input string?
It skips all leading spaces until it finds a non-space character to start conversion.
Click to reveal answer
intermediate
What should atoi do if the string contains non-digit characters after the number?
It stops parsing at the first non-digit character and returns the integer parsed so far.
Click to reveal answer
intermediate
How does atoi handle integer overflow or underflow?
It clamps the result to the 32-bit signed integer range: from -2,147,483,648 to 2,147,483,647.
Click to reveal answer
beginner
What is the output of atoi when the input string is empty or has no valid number?
It returns 0 because no valid integer could be parsed.
Click to reveal answer
What does atoi do with the string " -42abc"?
A-42
B42
C0
D-420
✗ Incorrect
It skips spaces, reads the sign '-', then parses digits until non-digit 'a' stops parsing, returning -42.
If the input string is "+12345", what integer does atoi return?
A12345
B-12345
C0
D1234
✗ Incorrect
The '+' sign indicates positive number, so it returns 12345.
What happens if atoi receives the string "words and 987"?
A0
B987
Cwords
D9870
✗ Incorrect
Since the string does not start with a number or sign, it returns 0.
How does atoi handle the string "2147483648" (one more than max 32-bit int)?
A2147483647
B2147483648
C0
D-2147483648
✗ Incorrect
It clamps the value to the max 32-bit signed integer 2147483647.
What is the result of atoi on an empty string ""?
A0
Bnull
Cundefined
DError
✗ Incorrect
No valid number found, so it returns 0.
Explain step-by-step how atoi converts the string " -0012a42" to an integer.
Think about how parsing stops at the first non-digit after digits.
You got /5 concepts.
Describe how atoi handles integer overflow and why it is important.
Consider the range -2,147,483,648 to 2,147,483,647.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the atoi function in programming?
easy
A. Check if a string contains only digits
B. Convert an integer into a string
C. Sort characters in a string alphabetically
D. Convert a string representing a number into an integer
Solution
Step 1: Understand the function name
atoi stands for ASCII to integer, meaning it converts strings to integers.
Step 2: Identify the function's purpose
It takes a string input and returns its integer value if possible.
Final Answer:
Convert a string representing a number into an integer -> Option D
Quick Check:
atoi = string to integer conversion [OK]
Hint: Remember atoi means ASCII to integer conversion [OK]
Common Mistakes:
Confusing atoi with integer to string conversion
Thinking atoi sorts strings
Assuming atoi validates string digits only
2. Which of the following JavaScript code snippets correctly converts the string "123" to an integer using a method similar to atoi?
easy
A. const num = Number.toString("123");
B. const num = parseInt("123");
C. const num = "123".toString();
D. const num = "123" + 0;
Solution
Step 1: Identify the correct conversion method
parseInt converts a string to an integer in JavaScript, similar to atoi.
Step 2: Check each option
const num = parseInt("123"); uses parseInt correctly. const num = "123".toString(); converts string to string, no change. const num = Number.toString("123"); is invalid syntax. const num = "123" + 0; concatenates string and number, resulting in a string.
Final Answer:
const num = parseInt("123"); -> Option B
Quick Check:
parseInt("123") = 123 [OK]
Hint: Use parseInt() to convert string to integer [OK]
Common Mistakes:
Using toString() which returns a string
Trying Number.toString() incorrectly
Adding 0 to string results in string concatenation
3. What will be the output of the following JavaScript code?
function myAtoi(str) {
let i = 0, sign = 1, result = 0;
while (str[i] === ' ') i++;
if (str[i] === '-' || str[i] === '+') {
sign = str[i] === '-' ? -1 : 1;
i++;
}
while (i < str.length && str[i] >= '0' && str[i] <= '9') {
result = result * 10 + (str[i].charCodeAt(0) - '0'.charCodeAt(0));
i++;
}
return sign * result;
}
console.log(myAtoi(" -42abc"));
medium
A. 0
B. 42
C. -42
D. NaN
Solution
Step 1: Skip leading spaces and check sign
The function skips spaces, finds '-', sets sign to -1, moves index forward.
Step 2: Convert digits until non-digit
Reads '4' and '2', converts to number 42, stops at 'a'.
Step 3: Apply sign and return
Returns -1 * 42 = -42.
Final Answer:
-42 -> Option C
Quick Check:
myAtoi(" -42abc") = -42 [OK]
Hint: Skip spaces, check sign, parse digits until non-digit [OK]
Common Mistakes:
Not skipping spaces before sign
Ignoring sign and returning positive number
Parsing non-digit characters
4. Identify the bug in this myAtoi function implementation:
function myAtoi(str) {
let i = 0, sign = 1, result = 0;
while (str[i] === ' ') i++;
if (str[i] === '-' || str[i] === '+') {
sign = str[i] === '-' ? -1 : 1;
i++;
}
while (i < str.length && str[i] >= '0' && str[i] <= '9') {
result = result * 10 + (str[i] - '0');
i++;
}
return sign * result;
}
medium
A. Subtracting characters directly without charCodeAt causes NaN
B. Missing check for empty string input
C. Sign is not applied correctly
D. Loop does not skip spaces
Solution
Step 1: Analyze digit conversion
Expression str[i] - '0' tries to subtract strings, which results in NaN in JavaScript.
Step 2: Correct approach
Use str[i].charCodeAt(0) - '0'.charCodeAt(0) to get numeric value of digit character.
Final Answer:
Subtracting characters directly without charCodeAt causes NaN -> Option A
Quick Check:
String subtraction needs charCodeAt() [OK]
Hint: Use charCodeAt() to convert char digits to numbers [OK]
Common Mistakes:
Subtracting characters directly causing NaN
Not handling empty or invalid input
Ignoring sign application
5. You want to implement myAtoi that converts a string to a 32-bit signed integer. Which approach correctly handles overflow and returns the clamped value if the number is out of range?
function myAtoi(str) {
let i = 0, sign = 1, result = 0;
const INT_MAX = 2147483647;
const INT_MIN = -2147483648;
while (str[i] === ' ') i++;
if (str[i] === '-' || str[i] === '+') {
sign = str[i] === '-' ? -1 : 1;
i++;
}
while (i < str.length && str[i] >= '0' && str[i] <= '9') {
let digit = str[i].charCodeAt(0) - '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;
}
hard
A. Correctly clamps overflow and returns 32-bit integer limits
B. Does not handle overflow, may return incorrect large numbers
C. Returns NaN on overflow instead of clamping
D. Ignores sign when clamping overflow values
Solution
Step 1: Check overflow condition
The code checks if adding the next digit causes overflow by comparing with INT_MAX/10 and last digit.
Step 2: Return clamped value
If overflow detected, returns INT_MAX or INT_MIN depending on sign, correctly clamping the value.
Step 3: Otherwise accumulate result
Accumulates digits safely within 32-bit integer range.
Final Answer:
Correctly clamps overflow and returns 32-bit integer limits -> Option A
Quick Check:
Overflow check and clamp = correct behavior [OK]
Hint: Check overflow before multiply, clamp to INT_MAX/INT_MIN [OK]
Common Mistakes:
Not checking overflow before multiplication
Returning wrong values on overflow
Ignoring sign when clamping
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.