Microsoft Interview Question for Software Engineer in Tests






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

I think the idea is that you can look into the problem of converting a number in decimal to a number in base 26 system (in this case numbers are A through Z).

StringBuffer sb = new StringBuffer();
while(n >= 0){
char current = numbers[n % 26];
n = n / 26 - 1;
sb = (new StringBuffer(current)).append(sb);
}

return sb.toString();

Here numbers is a char array that has A through Z from 0 to 25.

- Rytis November 02, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 0 votes

Gud One

- Anonymous November 02, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Rytis:
neat!

- burdell November 03, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

good solution!

- acoader November 03, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Gud job dude ! Excellent work

- Aryan November 03, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

A very nice thought, rytis...

- noviceprog November 05, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Slight modification to not require a char array

public void NumbertoExcelHeaders(int num)
{
    StringBuilder str = new StringBuilder();

    int digit = 0;
    while (num >= 0)
    {
        digit = num % 26;
        num =  (num/26) -1 ;

        str = str.Insert(0, (char)(digit + 'A'));

    }

    Console.WriteLine("{0}", str);
}

- anunay August 05, 2010 | Flag
Comment hidden because of low score. Click to expand.
1
of 0 vote

char * get_column(int column_no)
{
char *str="";

while(column_no>=0)
{
int no = column_no%26;
column_no = column_no/26-1;
char temp = 'A'+ (char)no;
char *temp1 = (char *)malloc(sizeof(char)*2) ;
temp1[0]= temp;
temp1[1]='\0';
str =strcat(temp1,str);
}

return str;
}

- cs_cougar November 02, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Did any one get the pattern for this ? ..
No
26*1 = 1 char name
from 27 to 26*[26] = 2 char name
677 to 26 * 26 * [26] = 3 char name .. and so on ..
I am struggling to get ahead of this ..

- p1 November 02, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This path is tempting, but harder to implement-
you can figure out the number of characters by doing:
ceiling(logN/log26), where N is the colum number (for simplicity, lets say the colum starts at 1 ). Let the number of characters be x. Then we need to create x counters. The xth counter moves fastest (i.e., go from A-Z, and then reset/repeat), the x-1th counter moves slower & so on...

The number of increments to make is N-(66^(x-1)), i.e after consuming all the combinations taking x-1 characters at a time, start with AAAAA... x times.

Runtime of this algorithm is O(N). Whereas the runtime of the better algorithm is O(log N) log is to the base 26.

- acoader November 03, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi Rytis, 2 questions
1. For 890(=AHG), your code will generate the rightmost char as numbers[890%26]=numbers[6]=F, whereas the char should be G.
2. What is the reasoning behind the -1 in
n = n/26 - 1 ?

Thanks

- Metta November 02, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Oops, missed the obvious. Never mind.

- Metta November 02, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. char temp = 'A'+ (char)no;
and devide with 26 and not 25.
that works i think.

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

Hi Everyone,

so why the (n = n/26 - 1) in Rytis's soln? Can anyone help out please?

- Vinod November 03, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Try to trace my algorithm with 26. The problem is that without -1 you're gonna get BA, whereas you want AA. That's why we need that -1.

- Rytis November 04, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Rytis solution was nice. Here is a recursive solution.

start the code with PrintSeq( c, 0, 0, 0, number);


PrintSeq( char a[], int h, int b, int num, int total) {

for (int i=0; i<26; i++) {
if (h > 0) {
c[h-1] = 'a' + b;
}
c[h] = alp[i];
num++;
if (num == total)
print(a) // The value in the array

}


if (b==25 || h==0) {
PrintSeq(c, h+1,0, num, total);
} else {
PrintSeq(c, h, b+1, num, total);
}
}

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

@Rytis - Impressive!

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

@Rytis

I am not very sure about the response by him . Let me give this by a small example

Rytis Code:

StringBuffer sb = new StringBuffer();
while(n >= 0){
char current = numbers[n % 26];
n = n / 26 - 1;
sb = (new StringBuffer(current)).append(sb);
}

return sb.toString();



StringBuffer sb = new StringBuffer();
if(n >= 0)
{
char current = number[n % 26];
// Now you have the current character.
// Find the number of times to append that charater
int times = n / 26 ;
while(times >= 0 )
{
sb = (new StringBuffer(current)).append(sb);
times = times - 1;
}
return sb.toString();
}


With example : If we have 53 as the integer given : Answer should be "BBB"

times = 53 / 26;
times = 2
We have the charater as from the number array as "B"

So it will return "BBB"

- Just_a_thought November 10, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I believe this works perfect. The logic is in counting, we would do 0-9 and the next number will be 10 and not 00. The logic Excel columns would do is 0-9 and 00, 01, 02 etc..

void ConvertToExcelColumn(int number, char * a)
{
char *ptr = a;
int n=0;

n = number%26;
*ptr = 'A' + n;
ptr++;
number = number/26;

while(number)
{
n = number%26;
*ptr = 'A' + n - 1;
ptr++;
number = number/26;
}

*ptr = '\0';

reverse(a);
}

- Anbe November 19, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Excel allows column names only till IV and so my code works fine! :)

- Anbe November 20, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public string ExcelColumnPrint(int number)
{
char[] aryABC = new char[] {'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'};
int bcNumber = number;
int bcNumber2 = number;
int reminder = 0;
Stack st = new Stack();
string column = string.Empty;

while (number > 25)
{
number = number / 26;
reminder = bcNumber % 26;
bcNumber = number;
st.Push(aryABC[reminder]);
}

if (bcNumber2 < 26)
st.Push(aryABC[number]);
else
st.Push(aryABC[number-1]);

while (st.Count > 0)
{
column += st.Pop().ToString();
}
return column;
}

- Raj November 23, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Little improvement:

public string ExcelColumnPrint(int number)
{
char[] aryABC = new char[] {'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'};
int bcNumber = number;
int reminder = 0;
StringBuilder sb = new StringBuilder();
string column = string.Empty;

while (number > 25)
{
reminder = number % 26;
number = number / 26;
sb.Insert(0, aryABC[reminder]);
}

if (bcNumber < 26)
sb.Insert(0, aryABC[number]);
else
sb.Insert(0, aryABC[number-1]);

return sb.ToString();
}

- Raj November 23, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

one can also ask following:
// Find the starting point of given digit
public int ExcelColumnSequenceStartPoint(int digit)
{
if (digit == 1) return 0;
int SeqStartPoint = 1;
while (digit-1 > 0)
{
SeqStartPoint = SeqStartPoint * 26;
digit--;
}
return SeqStartPoint;
}


// Find the range of given digit
public string ExcelSequenceRange(int digit)
{
return ExcelColumnSequenceStartPoint(digit).ToString() + " to " + string.Format("{0}",

(ExcelColumnSequenceStartPoint(digit+1) - 1));
}

- raj November 23, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The question I was asked was mapping from 1 .. 25 26 .. to A .. Z AA AB ... So the base is 1, not 0.

Then equation should be :
outChar = (n - 1) % 26 + 'A';
n = n / 26;

Prepare for being asked this.

- barry December 11, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sorry, should be
outChar = (n - 1) % 26 + 'A';
n = (n - 1) / 26;

to map from 1 .. 25 26 .. to A .. Z AA AB ...

- barry December 11, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include<string>
using namespace std;

string Translate(int N){
int n=N+1, m;
string a="";
do{
m=n%26;
n=n/26;
a.insert(a.begin(), m-1+'A');
}while(n);
return a;
}

void main(){
int num;
cout<<"Input an integer: ";
cin>>num;
cout<<"\n"<<Translate(num)<<" ";
}

- Siwen January 31, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Java flavor

int colnumber =1705;
		int more;
		int confirm;
		StringBuilder answer = new StringBuilder();
		more = colnumber/26;
		confirm = colnumber % 26;
		confirm = confirm  == 0 ? 26 : confirm;
		answer.insert(0, columnNames[confirm]);
		while(more > 26) {
			more = more/26;
			confirm = more % 26;
			answer.insert(0, columnNames[confirm]);
		}
		answer.insert(0, columnNames[more]);
		
		System.out.println(answer.toString());

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

no solution is correct till now

see the no :-
0 25 26 -----------------701 702.....728......754.....780
a---z aa zz aaa.. aba.. aca.. ada ..

no of algo work this way.

for code :

1) find out the bigger interval which the no number fills
that is 26 + 26*26 + 26*26*26
2) then take this many char as the interval no intialize them to 'A'

3) then take mode 26 and add in 'A' .

got the Idea ..if no then use some brain.

- nkb September 06, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

try 780 with the all algoes mentioned above ...it should give 'ADA' .

- nkb September 06, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Absolutely right! See code for this below

#include <stdio.h>

char * getB26(int num) {
	int temp = 0, cnt = 1, curr = num, i; 
	char * result;
	if (num < 1) return NULL;
	temp = 26;
	//find the interval
	while(num >=  temp) {
		num = num - temp;
		temp = temp * 26;
		cnt++;
	}
	result = (char *)malloc(cnt + 1);
	result[cnt] = 0;
	for(i = 0; i < cnt; i++)
		result[i] = 'A';
	while(num) {
		temp = num % 26;
		num = num/26;
		result[--cnt] = 'A' + temp;
	}
	return result;
}

int main(){
	char * res = getB26(780);
}

- Sarath November 22, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

char * extractColumn( int pos){

char *ptr;
char *ptrindex;
ptrindex = ptr;
// allocate some size to this string..
// Take some #define value
struct columnSet cSet;
while ( pos > 0) {
*(ptrindex) = pos%26 + 'A';
ptrindex++;
pos = pos/26;
}
*ptr = '\0';
return strrev(ptr);
}

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

#include <stdio.h>

char * getB26(int num) {
	int temp = 0, cnt = 1, curr = num, i; 
	char * result;
	if (num < 1) return NULL;
	temp = 26;
	//find the interval
	while(num >=  temp) {
		num = num - temp;
		temp = temp * 26;
		cnt++;
	}
	result = (char *)malloc(cnt + 1);
	result[cnt] = 0;
	for(i = 0; i < cnt; i++)
		result[i] = 'A';
	while(num) {
		temp = num % 26;
		num = num/26;
		result[--cnt] = 'A' + temp;
	}
	return result;
}

int main(){
	char * res = getB26(780); //prints ADA
}

- Sarath November 22, 2009 | 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