Bird
Raised Fist0
DSA Goprogramming~20 mins

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

Choose your learning style10 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 atoi conversion for input with leading spaces and sign?
Consider the input string " -42". What integer value does the atoi function return after processing this string?
DSA Go
func myAtoi(s string) int {
    i, sign, result := 0, 1, 0
    n := len(s)
    for i < n && s[i] == ' ' {
        i++
    }
    if i < n && (s[i] == '+' || s[i] == '-') {
        if s[i] == '-' {
            sign = -1
        }
        i++
    }
    for i < n && s[i] >= '0' && s[i] <= '9' {
        result = result*10 + int(s[i]-'0')
        i++
    }
    return sign * result
}
ASyntaxError
B42
C-42
D0
Attempts:
2 left
💡 Hint
Remember to skip spaces and check the sign before converting digits.
❓ Predict Output
intermediate
2:00remaining
What is the output when input string contains non-digit characters after number?
Given the input string "4193 with words", what integer does the atoi function return?
DSA Go
func myAtoi(s string) int {
    i, sign, result := 0, 1, 0
    n := len(s)
    for i < n && s[i] == ' ' {
        i++
    }
    if i < n && (s[i] == '+' || s[i] == '-') {
        if s[i] == '-' {
            sign = -1
        }
        i++
    }
    for i < n && s[i] >= '0' && s[i] <= '9' {
        result = result*10 + int(s[i]-'0')
        i++
    }
    return sign * result
}
A0
B4193
C4193 with words
D-4193
Attempts:
2 left
💡 Hint
Conversion stops at the first non-digit character after digits.
❓ Predict Output
advanced
2:00remaining
What is the output when input string represents a number exceeding 32-bit integer max?
Given the input string "91283472332", what integer does the atoi function return considering 32-bit signed integer limits?
DSA Go
func myAtoi(s string) int {
    i, sign, result := 0, 1, 0
    n := len(s)
    INT_MAX, INT_MIN := 2147483647, -2147483648
    for i < n && s[i] == ' ' {
        i++
    }
    if i < n && (s[i] == '+' || s[i] == '-') {
        if s[i] == '-' {
            sign = -1
        }
        i++
    }
    for i < n && s[i] >= '0' && s[i] <= '9' {
        digit := int(s[i] - '0')
        if result > (INT_MAX-digit)/10 {
            if sign == 1 {
                return INT_MAX
            } else {
                return INT_MIN
            }
        }
        result = result*10 + digit
        i++
    }
    return sign * result
}
A2147483647
B91283472332
C-2147483648
D0
Attempts:
2 left
💡 Hint
Check for overflow and clamp to 32-bit integer limits.
❓ Predict Output
advanced
2:00remaining
What is the output when input string has only non-digit characters?
Given the input string "words and 987", what integer does the atoi function return?
DSA Go
func myAtoi(s string) int {
    i, sign, result := 0, 1, 0
    n := len(s)
    for i < n && s[i] == ' ' {
        i++
    }
    if i < n && (s[i] == '+' || s[i] == '-') {
        if s[i] == '-' {
            sign = -1
        }
        i++
    }
    for i < n && s[i] >= '0' && s[i] <= '9' {
        result = result*10 + int(s[i]-'0')
        i++
    }
    return sign * result
}
A-987
B987
Cwords
D0
Attempts:
2 left
💡 Hint
If the first non-space character is not a digit or sign, return 0.
🧠 Conceptual
expert
2:00remaining
What is the main reason to check overflow before multiplying result by 10 in atoi implementation?
In the atoi function, why do we check if result > (INT_MAX - digit) / 10 before updating result = result * 10 + digit?
ATo prevent integer overflow which would cause incorrect results or wrap-around
BTo skip non-digit characters in the string
CTo handle negative numbers correctly
DTo optimize the code by avoiding unnecessary multiplication
Attempts:
2 left
💡 Hint
Think about what happens if the number becomes too large for the integer type.

Practice

(1/5)
1. What does the atoi function do in the context of strings and integers?
easy
A. Converts an integer into a string representation
B. Converts a string representing a number into an integer value
C. Checks if a string contains only digits
D. Splits a string into individual characters

Solution

  1. Step 1: Understand the purpose of atoi

    The function atoi stands for ASCII to integer, meaning it converts a string of digits into an integer number.
  2. Step 2: Compare options with definition

    Only Converts a string representing a number into an integer value correctly describes converting a numeric string to an integer value.
  3. Final Answer:

    Converts a string representing a number into an integer value -> Option B
  4. Quick Check:

    atoi converts string to int [OK]
Hint: Remember atoi means ASCII to integer conversion [OK]
Common Mistakes:
  • Confusing atoi with integer to string conversion
  • Thinking atoi splits strings
  • Assuming atoi validates string digits only
2. Which of the following Go code snippets correctly converts the string "1234" to an integer using strconv.Atoi?
easy
A. num, err := strconv.Atoi("1234")
B. num := strconv.Atoi("1234")
C. num := strconv.Atoi(1234)
D. num, err := strconv.Atoi(1234)

Solution

  1. Step 1: Check function signature of strconv.Atoi

    It returns two values: an integer and an error, so both must be captured.
  2. Step 2: Verify argument type

    The argument must be a string, so "1234" is correct, not 1234 (int).
  3. Final Answer:

    num, err := strconv.Atoi("1234") -> Option A
  4. Quick Check:

    Correct syntax captures int and error [OK]
Hint: Always capture both int and error from strconv.Atoi [OK]
Common Mistakes:
  • Not capturing the error return value
  • Passing an integer instead of a string
  • Using single return variable for two returns
3. What will be the output of the following Go code?
package main
import (
  "fmt"
  "strconv"
)
func main() {
  s := "-42"
  num, err := strconv.Atoi(s)
  if err != nil {
    fmt.Println("Error")
  } else {
    fmt.Println(num)
  }
}
medium
A. -42
B. 42
C. Error
D. 0

Solution

  1. Step 1: Understand input string and conversion

    The string "-42" is a valid integer representation including a negative sign.
  2. Step 2: Check error handling and output

    Since "-42" is valid, err will be nil and num will be -42, so it prints -42.
  3. Final Answer:

    -42 -> Option A
  4. Quick Check:

    Valid negative string converts to negative int [OK]
Hint: Negative strings convert correctly with atoi [OK]
Common Mistakes:
  • Assuming negative sign causes error
  • Expecting absolute value output
  • Ignoring error check
4. Identify the error in the following Go code snippet that uses strconv.Atoi:
package main
import (
  "fmt"
  "strconv"
)
func main() {
  s := "12a4"
  num, _ := strconv.Atoi(s)
  fmt.Println(num)
}
medium
A. Missing import of strconv package
B. Using fmt.Println instead of fmt.Print
C. Passing a string with letters to strconv.Atoi is allowed
D. Ignoring the error returned by strconv.Atoi

Solution

  1. Step 1: Analyze input string validity

    The string "12a4" contains a letter 'a', so it is not a valid integer string.
  2. Step 2: Check error handling in code

    The code ignores the error by using underscore _, so it prints zero value 0 without warning.
  3. Final Answer:

    Ignoring the error returned by strconv.Atoi -> Option D
  4. Quick Check:

    Always check error when converting strings [OK]
Hint: Never ignore error from atoi, always check it [OK]
Common Mistakes:
  • Assuming invalid strings convert to partial numbers
  • Ignoring error causes silent wrong output
  • Thinking fmt.Println usage is wrong
5. You want to convert a list of strings ["10", "-5", "abc", "20"] to integers using strconv.Atoi. Which approach correctly handles invalid strings and collects only valid integers?
hard
A. Use strconv.Atoi only on first string, ignore rest
B. Convert all strings without error check and append results directly
C. Loop over strings, convert each with strconv.Atoi, append to result only if error is nil
D. Convert strings to integers using strconv.Itoa

Solution

  1. Step 1: Understand the problem requirements

    We must convert multiple strings to integers and skip invalid ones safely.
  2. Step 2: Evaluate each option's correctness

    Loop over strings, convert each with strconv.Atoi, append to result only if error is nil correctly loops, converts, checks error, and appends valid integers only.
  3. Final Answer:

    Loop over strings, convert each with strconv.Atoi, append to result only if error is nil -> Option C
  4. Quick Check:

    Check error for each conversion before appending [OK]
Hint: Check error for each string before adding integer [OK]
Common Mistakes:
  • Appending invalid conversions without error check
  • Using strconv.Itoa which converts int to string
  • Ignoring invalid strings completely
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard