## Amazon Interview Question for Software Engineer / Developers

• 0

Country: India
Interview Type: Written Test

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

On questions like these, don't forget to handle cases where there is no binary equivalent.

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

in some cases while calculating binary equivalent for fractions , result continues ...how to handle that ?
8.5 will terminate at 1000.1 but
for 125.10 binary equivalent is 1111101.00011001100.........
where should we terminate in such cases?

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

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

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

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

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

Test cases:
Negative number.
Positive number.
Are white spaces allowed: " 123.45".

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

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

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

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

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

make it while((a/=2)>0) i missed 2
and i have not taken the case of error too

Comment hidden because of low score. Click to expand.
-1
of 1 vote

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

Output:
ERROR

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

correct binary equivalent of 125.10 is 1111101.0001100110........

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.