Today I attended an interview.
Just wanna share a good Q.
Rows are represented using alphabets.
Example:
first row - 'A'
second row: 'B'
.
.
.
26th row : 'Z'
27th: 'AA'
.
.
Now given a number, we need to find the corresponding alphabet
representation.
Say Given 78
Answer should be "BZ"
Given 26
answer should be "Z"
Given 27
answer should be "AA"
Solution:
Number of rows with single alphabet = 26. and range is 1 to 26
Number of rows with 2 alphabets = 26*26 and range is 27 to 702
...
IDEA:
Sum of n terms in GP = a(r^n-1)/(r-1)
long int GP_SUM(int i)
{
return 26(pow(26,i)-1)/(25);
}
char getChar(int i)
{
return "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i-1];
}
Given 1202.
1202 > GP_SUM(2) && 1202 <= GP_SUM(3)
So it should be 3 alphabet.
1202-GP_SUM(2) = 500.
Using 500 we need to compute the result.
500/pow(26,2) = 0, so last one is "A"
500/pow(26,1) = 19 with remainder 6. so second last one is "T" (20th char
because remainder is non zero)
remainder is 6 so third one is "F"
Ans:ATF.
Thank you.
Best Regards,
T V Thirumala Reddy
Engineer, Qualcomm India Private Ltd.
1540C30, 15th Floor, Building #9, Mindspace, Hitech city, Madhapur,
Hyderabad-81.
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.