Microsoft Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

void printString(int num){
int N = num;
int n;
String result ="";
boolean flag = false;
if(N >= 26) flag = true;
while(N != 0){
n = N % 26;
N= N/26;
if(N == 0 && flag){
n = n-1;
}
result = getchar(n)+result;

}
System.out.println("Result is :"+result);
}

public char getchar(int n){

char c = (char)(65+n);
return c;
}

- hotcoder October 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This code sample seems to work.

public static String convert(int n) {
        if (n < 0) {
            return ""; 
        }   

        int m = n % 26; 
        StringBuilder sb = new StringBuilder();
        sb.append((char)('A' + m));
        n = n/26; 

        while (n > 26) {
            m = n % 26; 
            sb.insert(0, (char)('A' + m - 1));
            n/=26;
        }   

        if (n > 0) {
            sb.insert(0, (char)('A' + n - 1));
        }   

        return sb.toString();

}

- nixfish October 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ExcelColumnGenerator {
	char[] alpha;
	int base;

	ExcelColumnGenerator(int base){
		this.base = base;
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i< base; i++){
             sb.append((char)('A'+i));
		}
		alpha = sb.toString().toCharArray();
	}
	public String column(int n){
        boolean flag = false;
        int r;
        StringBuffer sb = new StringBuffer();
		do{
			r = n%base;
			if(flag == false){
				sb.insert(0, alpha[r]);
				flag = true;
			}
			else{
				sb.insert(0, alpha[r-1]);
			}
			n = n/base;
		}while(n>0);
		
		return sb.toString();
	}
	public static void main(String[] args){
		ExcelColumnGenerator ecg = new ExcelColumnGenerator(26);
		for (int i=0;i<100;i++){
			System.out.println("n == " + i + " Column: "+ ecg.column(i));
		}
		System.out.println("n == " + 1000 + " Column: "+ ecg.column(1000));
	}
}

Output:
n == 0 Column: A
n == 1 Column: B
n == 2 Column: C
n == 3 Column: D
n == 4 Column: E
n == 5 Column: F
n == 6 Column: G
n == 7 Column: H
n == 8 Column: I
n == 9 Column: J
n == 10 Column: K
n == 11 Column: L
n == 12 Column: M
n == 13 Column: N
n == 14 Column: O
n == 15 Column: P
n == 16 Column: Q
n == 17 Column: R
n == 18 Column: S
n == 19 Column: T
n == 20 Column: U
n == 21 Column: V
n == 22 Column: W
n == 23 Column: X
n == 24 Column: Y
n == 25 Column: Z
n == 26 Column: AA
n == 27 Column: AB
n == 28 Column: AC
n == 29 Column: AD
n == 30 Column: AE
n == 31 Column: AF
n == 32 Column: AG
n == 33 Column: AH
n == 34 Column: AI
n == 35 Column: AJ
n == 36 Column: AK
n == 37 Column: AL
n == 38 Column: AM
n == 39 Column: AN
n == 40 Column: AO
n == 41 Column: AP
n == 42 Column: AQ
n == 43 Column: AR


n == 1000 Column: ALM

- sivabasava October 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

const int CHARS = 26;
char* column_to_string(int col){
    int n = col;
    int count = 0;
    int i = 0;
    while(n > 0){
        n /= CHARS;
        count++;
    }

    char* str = (char*)malloc(count + 1);
    str[count] = 0;
    n = col;
    for(i = count-1; i >= 0; i--){
        str[i] = 'A' + (n % CHARS);
        n /= CHARS;
        n--;
    }

    return str;
}

- Anonymous October 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ConvertNoToColHeader {
	
	public static void ConvAndPrint(int n){
		System.out.println(ConverNoToColHdr(n));
	}
	public static String ConverNoToColHdr(int n){
		if(n < 26){
			char mChar = (char)(65 + n);
			return Character.toString(mChar);
		}
		else
		{
			String strHeader = ConverNoToColHdr(n/26 - 1);
			strHeader += ConverNoToColHdr(n%26);
			return strHeader;
		}
		
	}

}
public static void main(String[] args) {
		String strColHeader;
		ConvertNoToColHeader.ConvAndPrint(10);
		ConvertNoToColHeader.ConvAndPrint(20);
		ConvertNoToColHeader.ConvAndPrint(25);
		ConvertNoToColHeader.ConvAndPrint(26);
		ConvertNoToColHeader.ConvAndPrint(27);
		ConvertNoToColHeader.ConvAndPrint(52);
		ConvertNoToColHeader.ConvAndPrint(53);
		ConvertNoToColHeader.ConvAndPrint(100);
		ConvertNoToColHeader.ConvAndPrint(700);
		ConvertNoToColHeader.ConvAndPrint(750);
		ConvertNoToColHeader.ConvAndPrint(800);
		ConvertNoToColHeader.ConvAndPrint(1000);
		ConvertNoToColHeader.ConvAndPrint(100000);
		
	}

- shyamal.pandya October 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

number system with base 26

- okaysh October 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class MS2 {

    public static void main(String[] args) {
        MS2 ms2 = new MS2();
        int index = 1000;
        String column = ms2.excelColumnString(index);
        System.out.println(column);
    }

    private String excelColumnString(int index) {
        String str = "";
        if(index<0) return str;
        int count=1, num=26, numOld=0;
        while(true){
            if(index>=num){
                count++;
                numOld=num;
                num = num+(int)Math.pow((double)num, count);
                System.out.println(num);
            }
            else break;
        }
        int remIndex = index-numOld;
        int alphabetIndex;
        System.out.println("remIndex: "+remIndex+", count:"+count);
        count--;
        int power;
        while(count>=0){
        power = (int)Math.pow(26,count);    
        alphabetIndex = remIndex / power;
        remIndex = remIndex % power;
        System.out.println("alphabetIndex:"+alphabetIndex+" remIndex:"+remIndex);
        str = indexToString(alphabetIndex,str);
        count--;
        }
        return str;
    }

    private String indexToString(int alphabetIndex, String str) {
        char c = (char) ('A'+alphabetIndex);
        str = str+c;
        return str;
    }
}

- Anonymous October 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

My code initially failed for large number when more that 2 characters required for column name. Adding some test data to check the code.

A - Z will be represented by 0-25
AA - ZZ will create 26*26 = 676 more numbers.

so AA-ZZ will be from 26 - 701

Number - Column name
701 - ZZ
702 - AAA
703 - AAB

* Order of complexity was also asked for the code.

- singhSahab October 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

my design
given no is n
n%26 store it in array one by one until it n become zero for example given no is 702 then
array[0]=702%26 is 0
702/26=27
now array[1]=27%26 is 1
27/26 =1
now array[2]=1%26 is 1
1/26 is 0
take array[2]array[1]array[0]which is 1 1 0 now subtract one from all but last will give you 0 0 0 now print AAA
another example 323
will give 12 11 subtract by one but last gives 11 11 which is LL thats all

- sukusuku October 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class excelChar {
	public void convertToString(int col) {
		StringBuilder sb= new StringBuilder();
		while(col!=-1) {
			sb.insert(0,(char)('A'+col%26));
			col=(col/26)-1;
		}
		System.out.println(sb.toString());
	}
	public static void main(String [] args) {
		excelChar e= new excelChar();
		e.convertToString(Integer.parseInt(args[0]));
	}
}

- Anonymous October 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

alpha contains A-Z from 0 to 25

public static void generate(char[] alpha, int num){
		int n=num;
		 int r;
		 Boolean flag=false;
		   StringBuffer sb = new StringBuffer();
			while(n>=26){
				r=n%26;
				if(flag)sb.insert(0, alpha[r-1]);
				else sb.insert(0, alpha[r]);
				n=n/26;
				if(!flag) flag=true;
			}
			if(flag)sb.insert(0, alpha[n-1]);
			else sb.insert(0, alpha[n]);
		   	System.out.println(num+" "+sb.toString());
	}

- coder October 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Simple implementation in C#

public static string GetStringRepresentation(int col)
{
    var result = @"";
    while (col >= 0)
    {
        var d = col % 26;
        col = (col / 26) - 1;
        result = ((char) ((d%26) + 65)) + result;
    }
    return result;

}

- kadspark October 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is just a small Python script I wrote. I am not sure if it works for all cases but worked for the cases I tried. Here I am running the program as <program name> <number>.

The ord () gives ascii value of character.
The chr () gives the ascii characted of the interger value.
string[::-1] does a string reverse.

Rest is just trivial.

#!/usr/bin/python
import sys
num = int(sys.argv[1])
s = ''
start = ord('A')
visited = False;

while (num > 0):
        temp = num % 26
        num = num / 26
        temp = temp + start
        if visited:
                temp -= 1
        visited = True
        s += chr(temp)

print s[::-1]

I am not a Python programmer so this might not be the best way to code this program.

- Saurabh October 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char* getColumn(int colunNumber){
    int count = 0;
    int n = colunNumber;
    do{
        count++;
        n=n/26;
    }while (n);
    char* name=(char*)malloc(count+1);
    if(NULL != name){
        name[count]='\0';
        for(int i=count-1;i>=0;i-- ){
            name[i] = 'A'+colunNumber%26;
            colunNumber = colunNumber/26;
            colunNumber--;
        }
    }
    return name;
}

- coder October 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int number = 1057889;
getColumnHeader(number);


static char[] data = { 'A', 'B', ….. 'Z' }

String getColumnHeader (int number) {

   String result = "";

   int i = number;
   for (; i >= 26; i = i/26) {
      result = data[i%26] + result;
  }

   result = data[i] + result;

   return result;
}

- aakk October 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String getExcelColumnName(int number) {
        int dividend = number + 1;
        int modulo;
        String columnName = "";
        
        while(dividend > 0) {
            modulo = (dividend - 1) % 26;
            char c = (char) (65 + modulo);
            columnName = Character.toString(c) + columnName;
            dividend = (dividend - modulo) / 26;
        }
        
        return columnName;

}

- Dinesh October 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here's a recursive approach. Assuming column 0 is A. and so on.

private static String getStringRepresentation(int num){
		char[] A={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
		StringBuilder output=new StringBuilder();
		
		if(num<26){
			output.append(A[num]);
			return output.toString();
		}
		int tmp=(num/26)-1;
		int rem=num%26;
		if(tmp<26){
			output.append(A[tmp]);			
		}
		else{
			output.append(getStringRepresentation(tmp));
		}
		output.append(A[rem]);
		
		return output.toString();
	}

- cijo.thomas October 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Excel
{
	public static char getChar(int num)
	{
			return (char)(65+num);
	}
	public static void main(String args[])
	{
		int num = Integer.parseInt(args[0]);
		String result = "";
		
		if(num>=26)
			result+=getChar((num/26)-1);
		result+=getChar(num%26);
		
		System.out.println("Result:"+result);
		
	}
}

- Anonymous November 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ExcelSheet {
	static char[] array;
	static {
		array = new char[26];
		for (int i = 0; i < 26; i++) {
			array[i] = (char) (i + 97);
		}
	}
	public static void convert(int n){
		System.out.println(convertInteger(n));
	}
	public static String convertInteger(int n) {
		if (n <= 0) {
			return "a";
		}
		if(n < 26){
			return ""+array[n];
		}
		int remainder = n % 26;
		if (remainder == 0) {
			return convertInteger((n - 1) / 26) + array[remainder];
		} else {
			return convertInteger(n / 26 - 1) + array[remainder];
		}
	}

	public static void main(String[] args) {
		convert(76);
	}

}

- Kevin March 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is my code:

public static string Get(int n)
        {
            if (n < 0) throw new ArgumentException();
            if (n == 0) return "A";

            var s = new StringBuilder();
            int i = n;
            while (i != 0)
            {
                s.Insert(0, (char)('A' + i % 26));
                i = i / 26;
            }
            if (s.Length > 1) s[0]--;
            return s.ToString();
        }

- SkyClouds March 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String excelNumToStrCol(int num){
		if(num < 0)
			return "";
		StringBuilder sb = new StringBuilder();
		while(num >= 0){
			sb.append((char)(num % 26 + 65));
			num /= 26;
			num --;
		}
		sb = sb.reverse();
		return sb.toString();
	}

- alex December 09, 2013 | 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