Challenge - 5 Problems
Atoi Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this atoi implementation with leading spaces and sign?
Consider the following Python function that converts a string to an integer. What will be printed when calling
my_atoi(" -42")?DSA Python
def my_atoi(s: str) -> int: i = 0 n = len(s) while i < n and s[i] == ' ': i += 1 sign = 1 if i < n and (s[i] == '+' or s[i] == '-'): sign = -1 if s[i] == '-' else 1 i += 1 num = 0 while i < n and s[i].isdigit(): num = num * 10 + int(s[i]) i += 1 return sign * num print(my_atoi(" -42"))
Attempts:
2 left
💡 Hint
Check how the function handles spaces and the sign before digits.
✗ Incorrect
The function skips leading spaces, detects the '-' sign, then parses digits to form the number 42, finally applying the negative sign.
❓ Predict Output
intermediate2:00remaining
What is the output when the string contains non-digit characters after number?
What will
my_atoi("4193 with words") print using the same function as before?DSA Python
def my_atoi(s: str) -> int: i = 0 n = len(s) while i < n and s[i] == ' ': i += 1 sign = 1 if i < n and (s[i] == '+' or s[i] == '-'): sign = -1 if s[i] == '-' else 1 i += 1 num = 0 while i < n and s[i].isdigit(): num = num * 10 + int(s[i]) i += 1 return sign * num print(my_atoi("4193 with words"))
Attempts:
2 left
💡 Hint
The function stops parsing when it encounters a non-digit character.
✗ Incorrect
The function parses digits until it hits a space, then stops and returns the number parsed so far.
🔧 Debug
advanced2:00remaining
What error does this atoi variant raise?
Consider this modified atoi function. What error will it raise when called with
my_atoi("+")?DSA Python
def my_atoi(s: str) -> int: i = 0 n = len(s) while i < n and s[i] == ' ': i += 1 sign = 1 if i < n and (s[i] == '+' or s[i] == '-'): sign = -1 if s[i] == '-' else 1 i += 1 num = 0 while i < n and s[i].isdigit(): num = num * 10 + int(s[i]) i += 1 return sign * num print(my_atoi("+"))
Attempts:
2 left
💡 Hint
Check what happens if no digits follow the sign.
✗ Incorrect
The function returns 0 because no digits are found after the '+' sign, so num remains 0.
❓ Predict Output
advanced2:00remaining
What is the output when input string is empty?
What will
my_atoi("") print using the same function?DSA Python
def my_atoi(s: str) -> int: i = 0 n = len(s) while i < n and s[i] == ' ': i += 1 sign = 1 if i < n and (s[i] == '+' or s[i] == '-'): sign = -1 if s[i] == '-' else 1 i += 1 num = 0 while i < n and s[i].isdigit(): num = num * 10 + int(s[i]) i += 1 return sign * num print(my_atoi(""))
Attempts:
2 left
💡 Hint
Empty string means no digits to parse.
✗ Incorrect
The function returns 0 because no characters are parsed and num stays 0.
🧠 Conceptual
expert2:00remaining
What is the main reason to check for integer overflow in atoi implementations?
Why do atoi implementations often check if the parsed integer exceeds 32-bit signed integer limits during conversion?
Attempts:
2 left
💡 Hint
Think about what happens if the number is too big for the data type.
✗ Incorrect
Checking overflow ensures the function returns a valid integer within the allowed range, preventing wrong results.