Microsoft Interview Question for SDETs


Team: Cloud
Country: United States




Comment hidden because of low score. Click to expand.
7
of 7 vote

int toInt(String str, int base){

		checkArgument(str != null, "'str' parameter is NULL");
		checkArgument(base > 1, "Incorrect 'base' passed");

		str = str.trim().toLowerCase();
		checkArgument(str.length() > 0, "Zero length/empty 'str' parameter passed");

		if( "-2147483648".equals(str) ){
			return Integer.MIN_VALUE;
		}

		int sign = 1;
		int index = 0;

		if( str.charAt(0) == '-' ){
			sign = -1;
			++index;
		}
		else if( str.charAt(0) == '+'){
			++index;
		}

		int res = 0;

		for( int i = index; i < str.length(); i++ ){
			char ch = str.charAt(i);

			int digit;

			if( Character.isDigit(ch) ){
				digit = ch - '0';
			}
			else {
				digit = 10 + (ch - 'a');
			}

			if( digit >= base ){
				throw new IllegalArgumentException("Incorrect str '" + str  +"', for base " + base);
			}

			res = res * base + digit;

			if( res < 0 ){
				throw new ArithmeticException("Overflow occurred for value '" + str + "'");
			}
		}

		return sign * res;
	}

- m@}{ January 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
9
of 9 votes

Coool m@}{ you provided the most complete solution !

- dumb fellow January 24, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

int ConvertStrToInt(string inputStr)
{
	int num = 0;
	char[] inputCharArray = inputStr.ToCharArray();
	foreach (char ch in inputCharArray)
	{
		num = num * 10 + (ch - '0');
	}
	return num;
}

- dumb fellow January 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

public static int ConvertToInteger(string number)
        {
            int sign = 1;
            int returnValue = 0;

            foreach (var digit in number)
            {
                if (digit == '+') continue;
                if (digit == '-')
                {
                    sign = -1;
                    continue;
                }

                if (digit < '0' || digit > '9')
                    throw new ArgumentException("String cannot have numbers!");

                returnValue = (returnValue * 10) + (digit - '0');
            }

            return returnValue * sign;
        }

- Asiful Haque Mahfuze February 10, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

private int parseNumber(String unparsedNumber) {
	final char[] unparsedNumberCharacters = unparsedNumber.toCharArray();

	int parsedNumber = 0;
	int currentPlace = 1;

	for (int i = (unparsedNumberCharacters.length - 1); i >= 0; i--) {
		parsedNumber += Character.getNumericValue(unparsedNumberCharacters[i]) * currentPlace;

		currentPlace *= 10;
	}

	return parsedNumber;
}

- Chris January 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class StringtoNumber {

	public static void main(String[] args){
		String num = "1234";
		int k =1;
		int sum =0;
		char ch[] = num.toCharArray();
		for(int i=ch.length-1;i>=0;i--){
			
			sum = sum + Character.getNumericValue(ch[i]) * k;
			k= k * 10;
			
		}
		System.out.println(sum);
	}

}

- umamahes January 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int convert_to_num(char *arr)
{
   int num = 0;
   int len = 0;
   int i =1;
   int is_negative = 0;
   char *tmp = arr;

   if(*tmp)
   {

     while(*tmp++)  /* len of string*/
        len++;

     tmp = arr;
     if(*tmp == '-') {
         len = len -1;  /*  first char is a sign for -ve */
         is_negative =1;
         tmp = tmp + len;
     }
     else
     {
         tmp = tmp + (len -1);  /* To last char in the list */
     }

     while(len--) {
        if(('0' <= *tmp) && ('9' >= *tmp))
        {
            num = num + ((*tmp - '0')*i);
            tmp--;
            i=i*10;
        }
        else
        {
            return 0;
        }
     }
   }
   if(num && is_negative) {
        num = ~num+1;
   }
   return num;
}

- Anonymous January 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class stringToInteger {

	public static void main(String[] args) {
		int y=1;
		int number = 0;
		int start = 0;
		int sign =1;
		String str = "9000";
		
		if(str.charAt(0) == '+'){
			start++;
			sign = 1;
		}
		else if(str.charAt(0) == '-'){
			start++;
			sign = -1;
		}
		for(int i=str.length()-1; i>=start;i--){
			char c = str.charAt(i);
			number = number + ((c-48)*y);
			y*=10;
			
		}
		number *= sign;
		
		System.out.println(number);
	}
}

- Joey January 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
void convert(char* in){
int num=0,i=0;
while(in[i]!='\0'){
num=num*10+(in[i]-'0');
i++;
}
printf("converted to decimal %d",num);
}
void main(){
char str[10]="1234";
convert(str);
}

- Prashanth Artal (ASU) January 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Stringtointeger{
public static void main(String args[]){
String s="12323456";
System.out.println(Integer.parseInt(s));
}
}

- Anonymous February 18, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String args[]){


StringstoInt();

}

static void StringstoInt(){
String str = "123456";
int a;

int len = str.length();
//System.out.println(""+len);

for(int i=0;i<len;i++){
a = str.charAt(i) - 48;
//System.out.println(""+i);
System.out.print(""+a);

}

- Anonymous March 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String args[]){
		
		
		StringstoInt();
		
	}
	
	static void StringstoInt(){
		String str = "123456";
		int a;
		
		int len = str.length();
		//System.out.println(""+len);
		
		for(int i=0;i<len;i++){
		 a =  str.charAt(i) - 48;
			//System.out.println(""+i);
		System.out.print(""+a);

}

- sai March 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assume if the input is not a numeric string then throw exception:

private int stringToInt(String s){
        if(isNumeric(s)) {
            long maxInt = Integer.MAX_VALUE;
            long minInt = Integer.MIN_VALUE;

            long stringValue = Long.parseLong(s);

            if (stringValue > maxInt || stringValue < minInt) {
                throw new ArithmeticException("String value : " + s + " is beyond integer range.");
            } else {
                return Integer.parseInt(s);
            }
        }
        else{
            throw new NumberFormatException("The String input is not valid.");
        }
    }

    private boolean isNumeric(String s){
        for(int i = 0; i<s.length(); ++i){
            try{
                Integer.parseInt(Character.toString(s.charAt(i)));
            }
            catch(NumberFormatException e){
                return false;
            }
        }
        return true;

}

- Anonymous March 11, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Well, since the question does not ask a particular way to solve just use int.TryParse

int ToInt(string input)
   int output;
    if( int.TryParse(input,out output))
      {
        return output;
      }
     throw new ArgumentException();
  }

- Daniel March 12, 2015 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More