Microsoft Interview Question
Country: India
Interview Type: In-Person
I dont understand this
Suppose if "AAA" is given should i take -> A+ AA, AA+ A , A+A+A etc
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
#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;
}
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");
}
}
/*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");
}
}
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;
}
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();
}
// 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;
}
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;
}
/*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;
}
works fine till AZ but then goes wrong
try BA should give 53
BB should give 54
BC should give 55
and so on.....
//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;
}
Base 26 representation
- Aashish June 30, 2012