Microsoft Interview Question for Software Engineers


Team: Cloud+Enterprise
Country: United States
Interview Type: Written Test




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

int main()
{
char *ptr = "1234";
int res = 0;
while(*ptr != '\0')
{
res = res*10 + *ptr - '0';
ptr++;
}
printf("%d", res);
return 0;
}

- Rndp13 April 21, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

1. How about negative numbers?
2. How about blank spaces within the string?
3. How about invalid chars within the string?
4. How about the int result overflows?

- ZG July 19, 2015 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

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;
	}

- varun April 23, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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.

- Javeed April 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

i guess interviewer wanted to see if you can think of different inputs like - "+1234", "-1234" etc.. and validations

- kn April 20, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

these are simple expansion cases using some basic knowledge of ascii.

- Javeed April 21, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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");
    }
}

- Scorpion King April 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

mSol.convert("+67889");

- xpoh April 21, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
}

- niraj.nijju April 21, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class GetIntValue{

public static void main(String[] args) {
String a="1233";
System.out.println("integer value " + retunIntValue(a));
}

/** returns the int value */
public static int retunIntValue(String n1) {
int intValue;
intValue =Integer.parseInt(n1);
return min;
}
}

- srinu.thota91 April 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// 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);
}

- boreal April 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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));
}
}

- Abhimanyu Chopra April 23, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Integer.parseInt(s, 10);

- houseUrMusic April 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

complex java solution

Integer.parseInt(s, 10);

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

#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");
}

- Arthur Câmara April 25, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#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");

}

- Arthur Câmara April 25, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- sonesh June 06, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static int ConvertToInt(string input)
        {
            int result = 0;
            int d = 1;
            for(int i=input.Length -1 ; i>=0; i--)
            {
                result +=  (input[i] - '0') * d;
                d = d * 10;
            }
            return result;
        }

- codealtecdown September 12, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

int toInt(String str){
    int n = 0;
    
    for (int i = 0; i < str.length(); i++){
        n *= 10;
        n += str.charAt(i) - '0';
    }

    return n;
}

- inucoder April 21, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i/p : -3 , h9k

- niraj.nijju April 21, 2015 | Flag


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