Amazon Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: Written Test
public static String printBinary(String n) {
int intPart = Integer.parseInt(n.substring(0, n.indexOf(‘.’)));
double decPart = Double.parseDouble( n.substring(n.indexOf(‘.’), n.length()));
String int_string = “”;
while (intPart > 0) {
int r = intPart % 2;
intPart >>= 1;
int_string = r + int_string;
}
StringBuffer dec_string = new StringBuffer();
while (decPart > 0) {
if (dec_string.length() > 32) return “ERROR”;
if (decPart == 1) {
dec_string.append((int)decPart);
break;
}
double r = decPart * 2;
if (r >= 1) {
dec_string.append(1);
decPart = r - 1;
} else
{dec_string.append(0);
decPart = r;
}
}
return int_string + “.” + dec_string.toString();
//fraction part i have terminated to 10 digits
input=input.replaceAll("[^0-9.]+", "E");
if(input.contains("E")||input.indexOf('.')!=input.lastIndexOf('.'))
System.out.println("ERROR");
else{
int i = input.indexOf(".");
int n = Integer.parseInt(input.substring(0, i));
StringBuffer binary=new StringBuffer();
while(n>0){
int j = n%2;
binary.append(j);
n=n/2;
}
binary = binary.reverse();
binary.append(".");
double f = Double.parseDouble(input.substring(i));
int count =0;
while(f>0&&count<10){
f=f*2;
input=""+f;
int p = input.indexOf(".");
int a = Integer.parseInt(input.substring(0, i));
f = Double.parseDouble(input.substring(p));
binary.append(a);
count++;
}
System.out.println(binary.toString());
Here is my approach.
Note that i have not handled the case of negative numbers.
int binaryD(char *bin,int n,int depth)
{
static int count;
if(n)
{
count++;
binaryD(bin,n/2,depth+1);
bin[depth]=(n&1)?'1':'0';
}
return count;
}
int binaryF(char* bin,float f,int *depth)
{
int count=0,i;
if(f<=0.0f)
return 1;
if(f==1.0f)
{
bin[*depth]='1';
return 1;
}
while(f)
{
f*=2;
if(f==1.0f)
{
bin[++*depth]='1';
return 1;
}
i=f;
if(f>1.0f)
{
bin[++*depth]='1';
f=f-i;
}
else
bin[++*depth]='0';
if(++count>32)
return 0;
}
}
void toBinary(char *s)
{
int num=0,i,depth=0,po=10,count;
char bin[50];
float f=0.0f;
for(i=0;s[i] && s[i]!='.';i++)
{
if(s[i]>='0' && s[i]<='9')
num=num*10 + s[i]-'0';
else
return;
}
if(s[i]=='.')
{
for(++i,po=10;s[i];i++,po*=10)
if(s[i]>='0' && s[i]<='9')
f=f+ (s[i]-'0')/(float)po;
else
return;
}
i=binaryD(bin,num,depth);
for(count=i-1;count>=0;count--)
printf("%c",bin[count]);
count=i;
if(f!=0.0f)
{
bin[i]='.';
binaryF(bin,f,&i);
for(;count<=i;count++)
printf("%c",bin[count]);
}
}
ideone.com/GElaE
Please let me know if any corner cases are missing
i// made decimal point limited to 10
conversion(char binary[])
float a;
scanf("%f",&a);
int int_part=(int)a,count=0,i=0;
float frac=a-int_part;
do{binary[i++]=a%2;}while((a/=)>0)
binary[i++]='.';
do{frac=frac*2;binary[i++]=(int)(frac);count++;}while(frac!=1.0f&&count<10);
public static void ConvertToBinary(string inputNumber)
{
string[] inputDigits = inputNumber.Split('.');
List<string> outputBinaryDigits;
bool IsValidInput = true;
Console.WriteLine("The Decimal Number is {0}", inputNumber);
if (inputDigits.Count() > 2)
{
Console.WriteLine("ERROR");
IsValidInput = false;
}
else
{
outputBinaryDigits = new List<string>();
int parseStatus = 0;
foreach (string digit in inputDigits)
{
int.TryParse(digit, out parseStatus);
if (parseStatus == 0)
{
Console.WriteLine("ERROR");
IsValidInput = false;
break;
}
else
{
outputBinaryDigits.Add(Convert.ToString(parseStatus, 2));
}
}
if (IsValidInput)
{
Console.WriteLine("The Binary Equivalent is {0}", string.Join(".", outputBinaryDigits));
}
}
}
Input : 125
Output:
The Decimal Number is 125
The Binary Equivalent is 1111101
Input: 125.10
Output:
The Decimal Number is 125.10
The Binary Equivalent is 1111101.1010
Input: 125.12.12
Output:
The Decimal Number is 125.12.12
ERROR
Input: 125.AD
Output:
The Decimal Number is 125.AD
ERROR
On questions like these, don't forget to handle cases where there is no binary equivalent.
- Gayle L McDowell May 23, 2012