Microsoft Interview Question

  • microsoft-interview-questions
    0
    of 0 votes
    24
    Answers

    WAP to return numbered index if input is excel sheet column header name.
    e.g
    excel sheet column headers are A, B, C , D ... Z, AA, AB...AZ, BA,, etc
    if Input is D , output should be 4
    and for AA output should be 27

    - gdb on June 30, 2012 in India Report Duplicate | Flag
    Microsoft Algorithm

Country: India
Interview Type: In-Person


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

Base 26 representation

- Aashish on June 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Base 26 with an exception of zero.

- ashot madatyan on June 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

void getIndex(void)
{
          char *str = "BC";   // just an example ... can test with "AAA" also
          int answer=0;
          int len = strlen(str) - 1;
        
          for(int i=0; len != -1 ; i++,len--)
          {
                   answer += ((int)pow(26,len)*(str[i] - 'A' +1));
           }      
}

- amber on July 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use hash map initially or you can directly do a binary search as the strings are sorted to get the index.
ans will be index+1

- anonymous on June 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I dont understand this

Suppose if "AAA" is given should i take -> A+ AA, AA+ A , A+A+A etc

- nishant on June 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Not exactly....there is no zero

- Anonymous on July 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is basically a number represented in base 26 with an exception that it has no 0 value (numbers go from 1 to 26 through).
So, to convert for example the "ABCD" to base 10 integer, just sum up as follows: pow(26,0)*('D'-'A' + 1) + pow(26,1)*('C'-'A' + 1) + pow(26,2)*('B'-'A' + 1) + pow(26,3)*('A'-'A' + 1).

See my implementation at codepad.org/3Xlsia9d

- ashot madatyan on June 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Consider A-Z as 26 base number system.
Convert it to 10Base number system

- nerd on June 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i = 0,s=0
notation(char *ptr)
s += (*ptr - 91 + 26 ) + i*26 ;//gives the coresponding number of character
i++
ptr++
return s

//can be done for both upper case or lower case

- softy on July 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<string>

int getStringIndex(char* str)
{
int len = strlen(str);
int pow = 1;
int index = 0;
for(int i = len-1; i>=0; i--);
{
index += (str[i] - 'A' + 1)*pow;
pow *= 26;
}
return index;
}

int main()
{
char str[] = "ABC";
int index = getStringIndex(str);
printf("Index = %d", index);
return 0;
}

- abc on July 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ExcelSheetColumnIndex {

public int indexOfExcelSheet(String indexString) {
int indexValue =0;
int i = 0;
if (indexString != null) {
if (indexString.length() == 1) {
indexValue = (int)indexString.charAt(0)-64;
} else if (indexString.length() > 1) {
i = (int)indexString.charAt(0)-64;
indexValue = 26*i+(int)indexString.charAt(0)-64;
}
}
System.out.println(indexValue);
return indexValue;
}

public static void main(String[] args) {
//char c = 'A';
//System.out.println((int)c);
ExcelSheetColumnIndex xl = new ExcelSheetColumnIndex();
xl.indexOfExcelSheet("BA");
}

}

- VSReddy on July 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*WAP to return numbered index if input is excel sheet column header name.
e.g excel sheet column headers are A, B, C , D ... Z, AA, AB...AZ, BA,, etc
if Input is D , output should be 4
and for AA output should be 27 */

public class ExcelSheetColumnIndex {

public int indexOfExcelSheet(String indexString) {
int indexValue =0;
int i = 0;
if (indexString != null) {
if (indexString.length() == 1) {
indexValue = (int)indexString.charAt(0)-64;
} else if (indexString.length() > 1) {
i = (int)indexString.charAt(0)-64;
indexValue = 26*i+(int)indexString.charAt(1)-64;
}
}
System.out.println(indexValue);
return indexValue;
}

public static void main(String[] args) {
ExcelSheetColumnIndex xl = new ExcelSheetColumnIndex();
xl.indexOfExcelSheet("BD");
}

}

- VSReddy on July 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think this will not work for AAA right

- Random on July 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

convert 26 base number into 10 base number

here is a pseudo code assuming that all characters are in caps...
int dec=0;
for(int j=0,i=strlen(input_str)-1;i>=0;i--){
dec+=((input_str[i]%65)+1)*pow(26, j);
j++;
}

- qaz on July 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I guess the logic depends on the length of the input string... Just assume input strings are consistent of capital A-Z

1. If input.length==0 -> error
2. If input.length==1 -> return (c - 'A' + 1)
3. If input.length>=2 -> return ((26^N * (c - 'A' +1) + 26^(N-1) * (c - 'A' +1)..... )

- TS on July 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If you have Visual Studio, try compiling followings....

static void Main(string[] args)
{
int a = GetIndex("BE");

Console.WriteLine(a);
}

public static int GetIndex(string input)
{
int index = 0;

if (input.Length <= 0)
{
return -1; // Indicates wrong input
}
else if (input.Length >= 1)
{
var charArray = input.ToCharArray();
int j = charArray.Length - 1;

for (int i = 0; i < charArray.Length; i++)
{
if (i == 0)
{
index += charArray[j--] - 'A' + 1;
continue;
}

index += Exp(26, i) * (charArray[j--] - 'A' + 1);
}
}

return index;
}

private static int Exp(int number, int exp)
{
for (int i = 1; i < exp; i++)
{
number *= number;
}

return number;
}

- TS on July 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int ConvertIndex(string index)
{
    if (string.IsNullOrEmpty(index))
    {
        return -1;
    }

    if (index.Length > 6)
    {
        // because "ZZZZZZ" works but "ZZZZZZZ" does not
        // you can add more accurate check here
        // or use long instead of int
        return -1;
    }

    var pow = 1;
    var result = 0;
    index = index.ToUpper();
    for (int i = index.Length - 1; i >= 0; --i)
    {
        if (index[i] < 'A' || index[i] > 'Z')
        {
            throw new ArgumentException("index");
        }

        checked
        {
            result += (index[i] - 'A' + 1) * pow;
            pow *= 26;
        }
    }

    return result;
}

public static void Test()
{
    Console.WriteLine(ConvertIndex("A"));
    Console.WriteLine(ConvertIndex("C"));
    Console.WriteLine(ConvertIndex("AA"));
    Console.WriteLine(ConvertIndex("AB"));
    Console.WriteLine(ConvertIndex("AAA"));
    Console.WriteLine(ConvertIndex("ZZZZZZ"));
    Console.WriteLine(ConvertIndex("ZZZZZZZ"));

    Console.ReadKey();
}

- Shtin on July 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// Program to return numbered index from column header
// A => 1
// AA => 27

size_t GetNumberedIndexFromHeader(__in const char *pszHeader)
{
	size_t nIndex = 0;
	if (nullptr == pszHeader)
	{
		return nIndex;
	}
	size_t nLength = strlen(pszHeader);
	for (int i = nLength - 1; (i>=0) && (pszHeader[i] != '\0'; --i)
	{
		ASSERT(pszHeader[i] >= 'A' && pszHeader[i] <= 'Z');
		size_t nCharVal = pszHeader[i] - 'A' + 1;
		nIndex = (nIndex * 26) + nCharVal;
	}
	return nIndex;
}

- Saranya on August 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here's the solution in Java. I've not handled any invalid input and stuff. In an actual interview, you must do that for sure. This is a simple base 26 system to base 10 system conversion. If you recollect your hex to decimal code you would've studied in school, you'll notice that the logic is the same here.

public static long numFromExcelCol(String excelCol)
	{
		long retVal = 0;
		for(char c : excelCol.toUpperCase().toCharArray())
		{
			retVal *= 26;
			retVal += c - 'A' + 1;
		}
		return retVal;
	}

- null on August 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

/*WAP to return numbered index if input is excel sheet column header name.
e.g  excel sheet column headers are A, B, C , D ... Z, AA, AB...AZ, BA,, etc
if Input is D , output should be 4
and for AA output should be 27     */

//        WORKING CODE TIME : O(n)

#include<stdio.h>
#include<string.h>
int main()
{
    int i=0,l,x,index=0;
    char S[5];
    printf("Enter Column header : ");
    gets(S);
    l=strlen(S);
    while(i<l)
    {
        x=S[i++];
        index+=x-'A'+1;
    }
    index+=25*(i-1);
    printf("%d",index);
return 0;
}

- niraj.nijju on June 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

works fine till AZ but then goes wrong
try BA should give 53
BB should give 54
BC should give 55
and so on.....

- student on June 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

//This is a modification of the above code.This should work fine.
#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
int i=0,l,x,index=0;
char S[5];
printf("Enter Column header : ");
gets(S);
l=strlen(S);
l=l-1;
while(i<=l)
{
x=S[i++];
index+=(x-'A'+1)*pow(26,l);
}
//index+=25*(i-1);
printf("%d",index);
return 0;
}

- Anonymous on June 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@anonymous : still erroneous .....just test is for BC

- student on July 01, 2012 | Flag


Add a Comment
Name:

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

Books

is a comprehensive book walking you through 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