I had a technical phone screen where I was asked to implement a string-to-integer conversion function.
Details
I was asked to implement a function that parses a string and converts it to an integer, without using built-in valueOf or similar functions.
The specific requirements were:
The string can have a negative sign ('-') at the beginning, but not a positive sign ('+'). If a negative sign is present, it must be the first character, representing a negative number. If the conversion results in an integer overflow, the function should throw an exception. The string should not contain any special characters.
If the string contains any special characters (e.g., letters), the function should throw an exception.
Leading zeros are allowed only if there is a single leading zero. If there are more than one leading zeros, the function should throw an exception. A negative sign is only valid at the beginning of the string. If it appears at any other position, the function should throw an exception.