Microsoft Interview Question
Software Engineer in TestsDid 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 ..
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.
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);
}
}
@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"
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);
}
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;
}
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();
}
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));
}
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());
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.
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);
}
#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
}
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).
- Rytis November 02, 2008StringBuffer 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.