0
0
DSA Pythonprogramming~20 mins

String to Integer atoi in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Atoi Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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"))
A-42
B42
C0
DSyntaxError
Attempts:
2 left
💡 Hint
Check how the function handles spaces and the sign before digits.
Predict Output
intermediate
2: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"))
A4193 with words
B0
C4193
DTypeError
Attempts:
2 left
💡 Hint
The function stops parsing when it encounters a non-digit character.
🔧 Debug
advanced
2: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("+"))
AValueError
BIndexError
C0
DNo error, returns 0
Attempts:
2 left
💡 Hint
Check what happens if no digits follow the sign.
Predict Output
advanced
2: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(""))
A0
BIndexError
CValueError
DNone
Attempts:
2 left
💡 Hint
Empty string means no digits to parse.
🧠 Conceptual
expert
2: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?
ATo convert the string faster using bitwise operations
BTo ensure the returned integer fits within the allowed range and avoid incorrect values
CTo speed up the parsing process by stopping early
DTo prevent the program from crashing due to memory overflow
Attempts:
2 left
💡 Hint
Think about what happens if the number is too big for the data type.