## Amazon Interview Question

• 0

Country: -
Interview Type: In-Person

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

``````void numchars(int nth)
{
int n = 1;

while(nth > pow(26,n))
{
n++;
}

cout<<"num of chars="<<n<<endl;
}

int main()
{
numchars(26);
numchars(676);
numchars(800);
return 0;
}``````

Comment hidden because of low score. Click to expand.
0

Instead of calling pow function, can't we just divide by 26 and keep and count and finally increment count by 1 if you have remainder in the last division operation (modulo fn)

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

``````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]);
}
}``````

Comment hidden because of low score. Click to expand.
0

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;
}``````

Comment hidden because of low score. Click to expand.
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

Comment hidden because of low score. Click to expand.
0

@babu,
you need to change your line
result[i++]=a[n%26 - 1];
to
result[i++]=a[(n-1)%26 ]; // n must be >0

In modulo arithmetic (n%26 - 1) is same as ((n-1)%26)
but the result we are using as an index, it has to be non-negative. (try for n=26, 26*26...)

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

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.

Comment hidden because of low score. Click to expand.
0

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.

Comment hidden because of low score. Click to expand.
0

I took A as 1, B as 2 and so on.

Comment hidden because of low score. Click to expand.
0

in approach 1 you mean quotient not remainder

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

int num = 706;
char str[10] = {0};
int i=0;
while( num )
{
str[i++] =(char) ((num % 26) + 64);
num /= 26;
}
strrev(str);

Comment hidden because of low score. Click to expand.
0

Try 26. It's wrong.

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

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);

Comment hidden because of low score. Click to expand.
0

If I am not wrong this code doesn't handle any N which is a multiple of 26. as N%26 will be 0 and char(0+ 64) is @.

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

// Only handles finding out the number of chars in the nth term. n is the input

int d = 26, cnt = 0;
while(n > 0) {
n = n - d;
d = d * 26;
cnt++;
}
printf("\n%d", cnt);

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

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);
}

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

The series is of form n + n^2 + n^3 + n^4 + ....

where n is 26

now apply the logic!

Comment hidden because of low score. Click to expand.
0

Its a GP, find n is straight GP Sum formulae

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

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

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;
}

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

// Using the simple i + I^2 + i^3+..

int numChars (int n) {

// nth element
int c=1; // number of chars
int i=26;

while (n > i) {
c++;
i = i + i ^c;
}

return c;

}

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

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";
}
}

Comment hidden because of low score. Click to expand.
0

The code should start from case 0, case 1.... case 25 as anything %26 is in the set [0,25].

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

I like this question, very similar to binary calculation

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

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();
}
}
}

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

``````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();
}``````

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

``````public static int numChar(int n) {
int count = 0, sub = 1;
while(n > 0) {
count++;
sub *= 26;
n -= sub;
}
return count;
}``````

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

``````public static void solution(int n) {
StringBuilder result = new StringBuilder();
int count = 0;
while(n > 0){
count++;
n--;
result.append((char) ('A' + n%26));
n /= 26;
}
System.out.println("number of char: " + count);
System.out.println(n+"th member: " + result.reverse().toString());
}``````

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.

### 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.