Amazon Interview Question
Country: -
Interview Type: In-Person
void Find(int num)
{
char a[26]="ABCDEFGHIJKLMNOPQRSTUVWXYZ", result[1000]={'\0'};
int i=0, n=num;
if(n <= 0)
{
printf("invalid number");
return;
}
while(n)
{
result[i++]=a[n%26 - 1];
n/=26;
}
printf("Number of characters in %d th member: %d\n",num, i-1);
printf("%d th member: ");
for(i--;i>=0;i--)
{
printf("%c",result[i]);
}
}
Your algorithm is wrong. You can try n = 26. This problem is not the same as number base conversion .
If you find that the numbers in each group of length is:
26, 26 * 26, 26 * 26 * 26,...
You will find the solution.
#include <iostream>
#include <string>
using namespace std;
string getNum(int num) {
int curgroup = 26;
int tot = 26;
int index = num;
while (tot < num) {
index -= curgroup;
curgroup *= 26;
tot += curgroup;
}
string ans;
while (curgroup > 26) {
int sg = curgroup / 26;
int ind = index / sg;
if (index % sg) ind++;
ans.push_back('A' + ind - 1);
index -= (ind - 1) * sg;
curgroup = sg;
}
ans.push_back('A' + index - 1);
return ans;
}
int main() {
int n;
while (cin >> n) {
cout << getNum(n) << endl;
}
return 0;
}
@flexme I dont think babu's algo is wrong.. there is some small problems, but i think the big picture is correct.
you two basically are using the same algo
Approach 1:
Divide the number 'n' with 26^1, if the remainder is <26 then return 1. if it is greater, then divide (n-26^1) with 26^2. if it is <26^2 return 2. else divide( n-26^1 - 26^2) with 26^3..... and so on.
So if the sequence has m characters, the last iteration computes (n-26^1-26^2...26^(m-1)) divided by 26^m.
Approach 2:
This is the easiest. find ceil((ln n)/(ln 26)).This is nothing but finding log n to the base 26. ceil of that gives the number of characters.
I think Approach 2 is not correct. For example, take n = 25 + 26*26, which is the index of string "ZZ" (if we let the index of "A" to be 0). The number of characters is 2, while ln n / ln 26 = 2.01....., whose ceil is 3.
Please have a look at below code and share your suggestion,,,
int num = 706;
char str[10] = {0};
int i=0;
while( num )
{
str[i++] =(char) ((num % 26) + 64);
num /= 26;
}
strrev(str);
In the above solution handle the case for input 26 .something like
int num = 1408;
char str[10] = {0};
int i=0;
while( num )
{
if ( num == 26) {
str[i++] = 'z';
break;
}
else{
str[i++] =(char) ((num % 26) + 64);
}
num /= 26;
}
strrev(str);
void Find(int num)
{
char ch[27]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char *pResult=NULL;
int count = 0;
int reminder = num % 26;
if(reminder)
count = num/26 + 1 ;
else
count = num/26 ;
pResult = (char*)malloc(count + 1); // count characters + NULL character
memset(pResult, 'A', count - 1);// count-1 numbers of 'A' will be there
pResult[count-1] = ch[reminder];
pResult[count] = '\0';
printf("Number of characters in nth member: %d & member is : %s", count, pResult);
}
int ReturnChar(int n)
/*
Find number of characters in nth member in the following series.
A,B,C,D....,AA,AB,......,ZY,ZZ,AAA,AAB,............
e.g for n<=26 chars are 1 for n=27 it is 2
Additional Question: Find nth member
here seq is getting generated 26,26^2,26^3 so on
*/
#include<math.h>
float y=26;
int ReturnChar(int n)
{
if(n==0)
return 0;
if(n<=26)
return 1;
return(1 + ReturnChar(ceil(n/y)));
}
int main()
{
int n;
scanf("%d",&n)
printf("Number of Chars=%d",ReturnChar(n));
return 0;
}
public static String findNth(int index){
String result="";
while(index>0){
int currentBit=index%26;
result=getWhat(currentBit)+result;
index=index/26;
}
return result;
}
public static String getWhat(int currentBit){
switch(currentBit){
case 1:
return "A";
case 2:
return "B";
case 3:
return "C";
case 4:
return "D";
case 5:
return "E";
case 6:
return "F";
case 7:
return "G";
case 8:
return "H";
case 9:
return "I";
case 10:
return "J";
case 11:
return "K";
case 12:
return "L";
case 13:
return "M";
case 14:
return "N";
case 15:
return "O";
case 16:
return "P";
case 17:
return "Q";
case 18:
return "R";
case 19:
return "S";
case 20:
return "T";
case 21:
return "U";
case 22:
return "V";
case 23:
return "W";
case 24:
return "X";
case 25:
return "Y";
default:
return "Z";
}
}
final static char[] chars = { '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' };
static String findNthNumber(int n) {
StringBuilder builder = new StringBuilder();
while (n > 0) {
int index = n % 26;
if (index == 0) {
index = chars.length;
n = n - 1;
}
builder.append(chars[index - 1]);
n = n / 26;
}
return builder.reverse().toString();
}
public static void main(String[] args) {
int max = 26 * 26 * 26 + 26 * 26 + 26;
for (int i = 1; i <= max; i++) {
System.out.print(findNthNumber(i) + " ");
if (i % 26 == 0) {
System.out.println();
}
}
}
final static char[] chars = { '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' };
public static void main(String[] args) {
int max = 26 * 26 * 26 + 26 * 26 + 26;
for (int i = 1; i <= max; i++) {
System.out.print(findNthNumber(i) + " ");
if (i % 26 == 0) {
System.out.println();
}
if (i == 26 * 26 + 26) {
System.out.println();
}
}
}
static String findNthNumber(int n) {
StringBuilder builder = new StringBuilder();
while (n > 0) {
int index = n % 26;
if (index == 0) {
index = chars.length;
n = n - 1;
}
builder.append(chars[index - 1]);
n = n / 26;
}
return builder.reverse().toString();
}
- jais.ashish August 29, 2012