String to Integer (atoi)

Asked atBitGo
1Give yourself 5 minutes
2Answer out loud, not in your head
3Then compare with the answer below

The problem

Implement the function myAtoi(s) which converts the given string s to a 32-bit signed integer (similar to the C/C++ atoi function).

Steps to Implement:

  • First,** ignore** any leading **whitespace **characters ' ' until the first non-whitespace character is found.
  • Check the next character to determine the sign. If it’s a '-', the number should be negative. If it’s a '+', the number should be positive. If neither is found, **assume **the number is positive.
  • Read the digits and convert them into a number. Stop reading once a** non-digit characte**r is encountered or the end of the string is reached. **Leading zeros **should be **ignored **during conversion.
  • The result should be **clamped **within the 32-bit signed integer range: [-2147483648, 2147483647]. If the computed number is outside this range, return -2147483648 if the number is less than -2147483648, or return 2147483647 if the number is greater than 2147483647.
  • Finally, return the computed number after applying all the above steps.

**Input: **s = " -12345" Output: -12345 Explanation:

  • Ignore leading whitespaces.
  • The sign '-' is encountered, indicating the number is negative.
  • Digits 12345 are read and converted to -12345.

Input: s = "4193 with words" Output: 4193 Explanation:

  • Read the digits 4193 and stop when encountering the first non-digit character (w).

Input: s = " +004500abc"

  • 0 ≤ s.length ≤ 200
  • s consists of English letters (both lowercase and uppercase), Digits '0' to '9', Characters: ' ', '+', '-', and '.'

cpp

class Solution {
public:
    int myAtoi(string input) {
        // Your code goes here
    }
};

java

class Solution {
    public int myAtoi(String input) {
        // Your code goes here
    }
}

python

class Solution:
    def myAtoi(self, input):
        # Your code goes here

javascript

class Solution {
    myAtoi(input) {
        // Your code goes here
    }
}

csharp

public class Solution
{
    public int MyAtoi(string input)
    {
        // Your code goes here
    }
}

go

func myAtoi(input string) int {
    // Your code goes here
}
Stuck? Show a way to structure it+
  1. 01Skip leading spaces.
  2. 02Read an optional sign.
  3. 03Accumulate consecutive digits only.
  4. 04Check overflow before multiply and add.
  5. 05Clamp to signed 32-bit bounds and return.

Reference answer

Then expect these follow-ups

  • How would you support arbitrary integer bases?

    Tests: follow-up reasoning

  • Why is checking after multiplication unsafe?

    Tests: correctness reasoning

Hiring now

BitGo has 44 open roles right now

landed applies to roles like these for you, on each company’s own site, with answers written for the role.

Get 5x interviews10 free auto-applies. No card.
  • Senior Software Engineer - Mobile Backend

    Toronto, Ontario, Canada

  • Backend Engineer E3 - India

    India

  • Backend Engineer E3 - Ecosystem

    India

Or practice this question first

Next question