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 the first non-space character to start parsing the number.
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 after the number 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 significance of the sign character in atoi?
The sign character (+ or -) determines whether the resulting integer is positive or negative. If no sign is present, the number is positive by default.
Click to reveal answer
What does atoi return when given the string " -42abc"?
A-42
B42
C0
D-4200
✗ Incorrect
The function skips spaces, reads the negative sign, parses digits until non-digit 'a', and returns -42.
If the input string is "4193 with words", what integer does atoi return?
A0
B4193
C4193 with words
D-4193
✗ Incorrect
Parsing stops at the first non-digit character after the number, so it returns 4193.
What should atoi return if the input string is "words and 987"?
A-987
B987
C0
Dwords
✗ Incorrect
Since the string does not start with a number or sign, the function returns 0.
How does atoi handle the input string "-91283472332" which is less than the 32-bit integer minimum?
A-2147483648
B-91283472332
C0
D2147483647
✗ Incorrect
The function clamps the value to the minimum 32-bit signed integer -2147483648.
What is the output of atoi for the input string "+123"?
A+123
B-123
C0
D123
✗ Incorrect
The plus sign indicates a positive number, so the output is 123.
Explain step-by-step how the atoi function converts the string " -0012a42" to an integer.
Think about how spaces, signs, digits, and letters are handled in order.
You got /5 concepts.
Describe how atoi manages integer overflow and underflow during conversion.
Remember the 32-bit signed integer range and what happens if the number is too big or too small.
You got /3 concepts.
Practice
(1/5)
1. What does the atoi function do when given a string input?
easy
A. Converts the string to a floating-point number
B. Reverses the string characters
C. Converts the string to an integer number
D. Counts the number of characters in the string
Solution
Step 1: Understand the purpose of atoi
The atoi function stands for ASCII to integer and converts a string representing a number into an integer value.
Step 2: Compare options with function behavior
Only Converts the string to an integer number correctly describes this conversion. Other options describe different operations unrelated to atoi.
Final Answer:
Converts the string to an integer number -> Option C
Quick Check:
atoi converts string to integer [OK]
Hint: Remember atoi means ASCII to integer conversion [OK]
Common Mistakes:
Thinking atoi converts to float
Confusing atoi with string reversal
Assuming atoi counts characters
2. Which of the following TypeScript function signatures correctly represents a simple atoi function?
easy
A. function atoi(str: string): number
B. function atoi(num: number): string
C. function atoi(str: number): string
D. function atoi(): void
Solution
Step 1: Identify input and output types for atoi
atoi takes a string input and returns a number (integer).
Step 2: Match function signature with correct types
function atoi(str: string): number matches this: input is string, output is number. Other options have wrong input/output types or no parameters.
Final Answer:
function atoi(str: string): number -> Option A
Quick Check:
Input string, output number [OK]
Hint: atoi input is string, output is number [OK]
Common Mistakes:
Swapping input and output types
Using no parameters
Using wrong parameter types
3. What is the output of the following TypeScript code?
function atoi(str: string): number {
let i = 0, num = 0, sign = 1;
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') {
num = num * 10 + (str.charCodeAt(i) - 48);
i++;
}
return num * sign;
}
console.log(atoi(" -42abc"));
medium
A. -42
B. 42
C. 0
D. NaN
Solution
Step 1: Skip leading spaces and detect sign
The code skips spaces, finds '-', so sign = -1, index moves past '-'.
Step 2: Parse digits until non-digit
Reads '4' and '2', converts to number 42, stops at 'a'.
Step 3: Apply sign and return
Returns 42 * -1 = -42.
Final Answer:
-42 -> Option A
Quick Check:
Parsed number with sign = -42 [OK]
Hint: Skip spaces, check sign, parse digits until non-digit [OK]
Common Mistakes:
Not skipping spaces
Ignoring sign character
Parsing non-digit characters
4. The following TypeScript atoi implementation has a bug. What is the bug?
function atoi(str: string): number {
let num = 0;
let i = 0;
while (i < str.length && str[i] === ' ') i++;
for (; i < str.length; i++) {
if (str[i] < '0' || str[i] > '9') break;
num = num * 10 + (str.charCodeAt(i) - 48);
}
return num;
}
medium
A. Returns string instead of number
B. Does not handle leading spaces
C. Does not stop parsing at non-digit characters
D. Does not handle negative or positive signs
Solution
Step 1: Check for sign handling
The code does not check for '+' or '-' signs before parsing digits.
Step 2: Verify other behaviors
It skips leading spaces, stops parsing at first non-digit and returns number, so no issue there. The main bug is missing sign handling.
Final Answer:
Does not handle negative or positive signs -> Option D
Quick Check:
Missing sign handling in atoi [OK]
Hint: Check if signs '+' or '-' are handled [OK]
Common Mistakes:
Assuming spaces are main bug
Thinking it parses non-digit characters
Confusing return type
5. You want to extend the atoi function to handle integer overflow by clamping the result between INT_MIN = -2147483648 and INT_MAX = 2147483647. Which code snippet correctly clamps the value before returning?
const INT_MIN = -2147483648;
const INT_MAX = 2147483647;
function atoi(str: string): number {
let i = 0, num = 0, sign = 1;
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') {
num = num * 10 + (str.charCodeAt(i) - 48);
i++;
}
num = num * sign;
// Clamp here
// ???
return num;
}
hard
A. if (num > INT_MIN) return INT_MIN; if (num < INT_MAX) return INT_MAX;
B. if (num < INT_MIN) return INT_MIN; if (num > INT_MAX) return INT_MAX;
C. if (num < INT_MAX) return INT_MIN; if (num > INT_MIN) return INT_MAX;
D. if (num === INT_MIN) return INT_MAX; if (num === INT_MAX) return INT_MIN;
Solution
Step 1: Understand clamping logic
If number is less than INT_MIN, return INT_MIN; if greater than INT_MAX, return INT_MAX.
Step 2: Match correct condition and return
if (num < INT_MIN) return INT_MIN; if (num > INT_MAX) return INT_MAX; correctly checks and returns clamped values. Other options have incorrect comparisons or swap min/max.
Final Answer:
if (num < INT_MIN) return INT_MIN; if (num > INT_MAX) return INT_MAX; -> Option B
Quick Check:
Clamp below min and above max correctly [OK]
Hint: Clamp with if less than min or greater than max [OK]
Common Mistakes:
Swapping INT_MIN and INT_MAX in conditions
Using equality instead of inequality
Returning wrong clamp value
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.