Microsoft Interview Question
Software EngineersTeam: Cloud+Enterprise
Country: United States
Interview Type: Written Test
private static int convert(String s) {
int res = 0;
int i =0;
boolean isNegative = false;
if(s.length() ==0 ){
System.out.println("Invalid string");
return res;
}
if(s.toCharArray()[0] == '-'){
isNegative = true;
i++;
}
for(;i<s.length();i++){
res = 10*res +Character.getNumericValue(s.toCharArray()[i]);
}
if(isNegative){
res=-res;
}
if(res >Integer.MAX_VALUE){
System.out.println("Bigger than max int val");
return Integer.MAX_VALUE;
}
if(res <Integer.MIN_VALUE){
System.out.println("smaller than min int val");
return Integer.MIN_VALUE;
}
return res;
}
this is a classic, with some basic ascii conversion you can do this in one run.
in python:
def get_int_val(word):
output = 0
for i in range(0, len(word)):
mult_val = 10**(len(word) -1 - i)
x = ord(word[i])-48
if x >= 0 and x <= 9:
output += mult_val*x
return output
this will fill any spots taken up by chars with 0's, it can easily be modified to not do that. The logic is to use the base-10 property of the integer to get properly placed values added to the sum and returned. O(n) time.
i guess interviewer wanted to see if you can think of different inputs like - "+1234", "-1234" etc.. and validations
class SolutionConvertStringToInteger {
public void convert(String str) {
int negFlag = 1;
int start = 1;
int out = str.charAt(0) - '0';
if(str.charAt(0) == 45) {
negFlag = -1;
out = str.charAt(1) - '0';
start = 2;
if(out < 0 || out > 9) {
System.out.println("INVALID NUMBER");
return;
}
}
for(int i = start; i < str.length(); i++) {
int temp = str.charAt(i) - '0';
if(temp < 0 || temp > 9) {
System.out.println("INVALID NUMBER");
return;
}
out = (out * 10) + temp;
}
System.out.println(out * negFlag);
}
}
public class ConvertStringToInteger {
public static void main(String[] args) {
SolutionConvertStringToInteger mSol = new SolutionConvertStringToInteger();
mSol.convert("67889");
mSol.convert("-67889");
mSol.convert("-678-89");
mSol.convert("678-89");
}
}
public static Integer convert(String word){
int index = 0;
if(0==word.length() )
return null;
int num=0;
boolean isNegative = false;
char [] chWords = word.toCharArray();
if(chWords[index]=='-' || chWords[index]=='+'){
if(chWords[index]=='-')
isNegative = true;
index++;
}
for(;index < chWords.length; index++){
if(chWords[index]<'0' || chWords[index]>'9'){
System.out.println("Invalid input");
return null;
}else{
num=num*10 + chWords[index]-'0' ;
}
}
if(isNegative)
num *=-1;
return num;
}
// pStr = "3456"
int my_atoi(char *pStr)
{
int val = 0;
UINT len = strlen(pStr);
UINT index = 0;
UCHAR curr_char = 0;
for (int i = len-1; i >=0; i--) {
if (pStr[i] == '-') { continue; }
printf("i is %d\n", i);
curr_char = pStr[i];
int curr_val = ((int) curr_char) - '0';
val += (curr_val * pow(10, index++));
}
if (pStr[0] == '-') { val = 0 - val; }
return(val);
}
class StringToInteger{
static int stringToInteger(String str)
{
char ch=' ';
int no=0,temp=0,len=str.length();
for(int i=0;i<len;i++)
{
ch=str.charAt(i);
if(Character.isDigit(ch))
{
temp=Character.getNumericValue((int)ch);
no=no+temp;
no=no*10;
}
else
{
no=0;
System.out.println("Please enter only numeric values");
System.exit(0);
}
}
no=no/10;
return no;
}
public static void main(String[] args)
{
String str="12a34";
System.out.println("Integer No is: "+stringToInteger(str));
}
}
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int my_atoi(string str){
int counter=str.size();
int ret=0;
for (unsigned it = 0; it<str.size(); ++it) {
cout<<str[it]<<endl;
ret+=pow(10, counter-it-1)*(str[it]-'0');
cout<<ret<<endl;
}
return ret;
}
int main(int argc, char *argv[]) {
cout<<my_atoi("1234");
}
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int my_atoi(string str){
int counter=str.size();
int ret=0;
for (unsigned it = 0; it<str.size(); ++it) {
cout<<str[it]<<endl;
ret+=pow(10, counter-it-1)*(str[it]-'0');
cout<<ret<<endl;
}
return ret;
}
int main(int argc, char *argv[]) {
cout<<my_atoi("1234");
}
here is the solution
ConvertToInt(inputStr)
length <- inputStr.Length
if length > k // k is the size of the maximum integer
Overflow;return;
multiplier = 1;
output = 0;
maxLastNum = r;
for i = length; j > 1; j --
output = output*multiplier + Int(inputStr[i]);
multiplier = multiplier * 10;
if Int(inputStr[length]) > maxLastNum OR Int(inputStr[length])*multiplier > MaxInt-output
Overflow;return;
else
output = output*multiplier + Int(inputStr[length]);
return output
int main()
- Rndp13 April 21, 2015{
char *ptr = "1234";
int res = 0;
while(*ptr != '\0')
{
res = res*10 + *ptr - '0';
ptr++;
}
printf("%d", res);
return 0;
}