Amazon Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: Phone Interview
Can be solved use a recursive method:
import java.util.HashMap;
public class FindTheKthString {
private static HashMap<Integer, StringBuffer> letterCode = new HashMap<Integer, StringBuffer>();
static {
for (int i = 1; i < 26; i++) {
letterCode.put(i, new StringBuffer(Character.toString((char) (i + 96))));
letterCode.put(0, new StringBuffer(Character.toString((char) (26 + 96))));
}
}
private static StringBuffer getTheStringAtKthPosition(int i) {
int lastIndex = i%26;
int step = (i-1)/26;
if(step==0)
{
return new StringBuffer(letterCode.get(lastIndex));
}
else
{
return getTheStringAtKthPosition(step).append(letterCode.get(lastIndex));
}
}
public static void main(String[] args) {
for(int index=700;index<=740;index++)
{
System.out.println(index+" "+getTheStringAtKthPosition(index));
}
}
}
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
public class abcdQuestion {
public static void main(String[] args) {
printString(28);
}
public static void printString(int k) {
if (k <= 26) {
System.out.println(Character.toString((char) (96 + k)));
return;
}
HashMap<Integer, String> hp = new HashMap<Integer, String>();
HashMap<Integer, String> temp = new HashMap<Integer, String>();
for (int i = 1; i <= 26; i++) {
hp.put(i, Character.toString((char) (i + 96)));
}
int count = 27;
int l = 1;
while (count <= k) {
String s = hp.get(l);
Iterator<Entry<Integer, String>> it = hp.entrySet().iterator();
while (it.hasNext() && count <= k) {
String d = s + it.next().getValue();
temp.put(count, d);
count++;
}
hp.putAll(temp);
if ((count - 1) == k) {
System.out.println(hp.get(k));
return;
}
l = l + 1;
}
}
}
#include <stdio.h>
#include <stdlib.h>
int main(){
int ar[10];
int n,t,q,r,k,i;
scanf("%d",&n);
k=0;
r = n%26;
ar[k] = r;
q = n/26;
while(q != 0){
k++;
r = q%26;
ar[k] = r;
q = q/26;
}
printf("Binary:\n");
t=k;
while(t >= 0)
printf("%d | ",ar[t--]);
while(k >= 0){
i=k-1;
if(ar[i] != 0){
t = ar[k--] + 96;
printf("%c",t);
}
else{
if(ar[k] > 1){
t = ar[k] + 95;
printf("%c",t);
}
t = 26 + 96;
printf("%c",t);
k=k-2;
}
}
return;
}
Assumption user will enter +ve value starting from one.
void InterviewQuestions::findValueInSequence()
{
std::string outputStr = "a";
int positionRequested = 28;
std::cout<<"\nEnter the position requested (+ve number only starting from one) : ";
std::cin>>positionRequested;
// Decrement as value is calculated as per the index 0;
--positionRequested;
int i = 0;
int n = positionRequested/26;
int rem = positionRequested%26;
while ( i < n )
{
if (outputStr.size() == 1)
{
outputStr.insert(0,1,'a');
}
else
{
char ch = outputStr[0];
if ( ch == 'z')
{
outputStr[0] = 'a';
outputStr.insert(0,1,'a');
}
else
{
ch += 1;
outputStr[0] = ch;
}
}
i++;
}
// add the reminder to the first character of the output string.
char ch = outputStr[outputStr.size() - 1];
for ( i = 0; i< rem; i++)
{
++ch;
}
outputStr[outputStr.size() - 1] = ch;
std::cout<<"\nRequested Character is : "<<outputStr.c_str()<<"\n";
}
Trie can help here.
given value (say n).
n/26=x and n%26=y.
now what U hv to go is to the x level node and yth element. that O(1) complexity
I believe this is the answer they were looking for:
int num = 28;
char letter;
string answer;
while(num)
{
letter = (num-1)%26;
answer.push_back(letter + 'a');
num = (num-letter)/26;
}
You then must print in reverse order due to the way it is pushed onto the string...
I made a small change in your program as it wasn't behaving well for numbers greater than 676
int num = 704;
int letter;
String answer="";
while(num != 0)
{
letter = (num)%26;
answer = answer + ((char)(letter+96));
num = (num-letter)/26;
}
StringBuffer buffer = new StringBuffer(answer);
answer = buffer.reverse().toString();
char[] array = answer.toCharArray();
for(int i =0;i<array.length;i++) {
if(i > 0 && i < answer.length()-1) {
int val =(int)array[i] +1;
array[i] = (char)val;
}
}
for(int i =0;i<array.length;i++) {
System.out.print(array[i]);
}
def getStringAtKth(k):
if k<=26:
return chr(96+k)
q,r = divmod(k,26) #q-> quotient and r-> remainder
if r == 0:
q, r = q-1, 26
retstr = getStringAtKth(q)+chr(96 + r)
return retstr
Submitted by me without login......
def getStringAtKth(k):
if k<=26:
return chr(96+k)
q,r = divmod(k,26) #q-> quotient and r-> remainder
if r == 0:
q, r = q-1, 26
retstr = getStringAtKth(q)+chr(96 + r)
return retstr
It is much like a 2D array
[ a b c d z]
[ aa ab ac ad az]
[ ba bb bc bd bz]
[---------------------]
[---------------------]
[ za zb zc zd zz]
let XY is the string at kth position
X can be generate with k/26(0-null, 1-a, 2-b........26-z )
Y can be generate with k%26(0-a, 1-b, 2-c........25-z )
----Prefer algo rather then code in this forum
public class Base26 {
public static void main(String[] a) {
String alphabet = "zabcdefghijklmnopqrstuvwxy";
String result = new String();
int num = 26*26*26*26;
if(num == 0) {
System.out.print("a");
} else {
while(num > 0) {
int digit = num%26;
result = alphabet.charAt(digit) + result;
if(digit == 0) {
num = (num-1)/26;
} else {
num = num/26;
}
}
System.out.print(result);
}
}
}
I not sure of the solution. Do a simple conversion as we do in Decimal to binary with base as 26 instead if 2.
eg: If k is 1408, then
26| 1408
--------
26| 54 - 4
--------
26| 2 - 2
---------
So the solution is 224 and converting it into alphabets gives bbd.
For given eg, k=28 => 12 => ab
Pls correct me
Base 26 seems to be the best way here. But I wanted to know how the number say k=678 would map to the string aab. 678 would be nothing but 102 base 26. So, how can this equates to aab?
It's a base 26 conversion Here is the Java solution:
public static void main(String args[]) {
int position = 85;
String result = "";
while (position > 0) {
int b = position % 26;
position = position / 26;
result = b + result;
}
String charResult = "";
for (int iCount = 0; iCount < result.length(); iCount++) {
charResult = charResult + (char) (result.charAt(iCount) - 47 + 95);
}
System.out.println("String is: " + charResult);
}
{
#include<stdio.h>
int main()
{
int n=703;
char arr[26]={'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'};
while(n){
printf("%c ",arr[(n)%26]);
n=(n)/26;
}
return 0;
}
}
guys is this as simple as this or am i missing something .... of course the above program prints the required atring in reverse we can avoid that by using recursion or store it in a string to reverse it ...
public String encode(final int number) {
StringBuilder btr = new StringBuilder();
int quotient = number;
int remainder = 0;
while(quotient != 0) {
remainder = quotient % base;
if(remainder == 0)
remainder = base;
quotient = (quotient - remainder) / base;
char c = (char)('a' + remainder - 1);
btr.append(c);
}
return btr.reverse().toString();
}
This question can be reduced to converting a decimal number (base 10) to a base 26 number.
Below is the code for it :
#include <iostream>
using namespace std;
int getLength(int number, int base)
{
int length = 0;
while(number > 0 )
{
number /= base;
length++;
}
return length;
}
int* convertDecimal(int toBase, int number)
{
int len = getLength(number,toBase);
int *base26 = new int[len];
int remainder;
for(int i=0; i < len ; i++)
{
base26[i] = number % toBase;
number /= toBase;
}
return base26;
}
int main()
{
int number ;
cout<<"Enter the number : ";
cin>>number;
int *base26 = convertDecimal(26,number-1);
int length = getLength(number-1,26);
for(int i=length-1; i>=0; i--)
{
cout<<" "<<char(97+base26[i]);
}
cout<<"\n\n";
system("pause");
}
Number of a's = (int)k/26;
- ravigupta May 17, 2012Terminating Charter = k%26, where
1 = a
2 = b
.
.
.
26 = z
So, if k=28
No. of a's = (int)28/26 = 1
Terminating Character = 28%26 = 2
Hence result = ab