Amazon Interview Question for SDE1s


Country: India
Interview Type: Written Test




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

public class decimaltobinary {
	public static String printBinary(double num){
		StringBuilder binary = new StringBuilder();
		int intNum = (int)num;
		int count = 0;
		while (intNum != 0){
			binary.append(intNum %2);
			intNum = intNum /2;
		}
		binary.reverse(); // due to the conversion method, the binary is in backwards order
		binary.append(".");
		num = num - (int)num; // remove the numbers before the decimal, as they have already been converted.
		
		
		while (num > 0 || count < 4){
			if (count == 4){ break;}
			if (num <= 0) {
				binary.append(0);
			} else {
				double r = num * 2;
				if (r >= 1){ 
					binary.append(1);
					num = r - 1;
				} else {
					binary.append(0);
					num = r;
				}
			}
			count++;
			
		}
			
		
		
		return binary.toString();
	}
	public static void main(String[] args) {
		double numcheck = 42.3333333; // used solely for testing -- can be easily replaced with user input 
		System.out.println(String.valueOf(numcheck)+ "\t\t" +printBinary(numcheck));

	}

}

Here is my implementation using Java. You could always use Integer.toBinaryString(int) to convert the number before the decimal, but I believe that would be considered cheating in the form of a test/interview. Feel free to comment.

- braaap June 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def dec2bin(number):
    ## integer part:
    int_number = int(number)
    int_bin_str = ''
    while int_number != 0:
        (int_number, remainder) = divmod(int_number, 2)
        int_bin_str = str(remainder) + int_bin_str

    ## fractional part
    frac_number = number - int(number)
    frac_bin_str = ''
    count = 0
    while( count < 4):
        frac_bin_str += str(int(2.0 * frac_number))
        frac_number  = 2*frac_number - int(2*frac_number)
        count += 1

    return int_bin_str+"."+frac_bin_str

## MAIN ##
print dec2bin(10)
print dec2bin(2.22)
print dec2bin(0.876)

- whatevva' June 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <string>

void dectobin(const unsigned int& num)
{
    unsigned int n = num;
    std::string strout;

    while (0 != n) {
        if ((1 & n) == 1) {
            strout.insert(0, "1");
            printf("1\n");
        }
        else {
            strout.insert(0, "0");
            printf("0\n");
        }
        n = n >> 1;
    }

    printf("Decimal: %u Binary: %s\n", num, strout.data());
}

int main(int argc, char* argv[])
{
    int num = 120;
    dectobin((unsigned int)num);
    return 0;
}

- ashot madatyan June 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about this................

#include<iostream>
#include<string>
using namespace std;
int main(){
    unsigned int n=10;
    string s;
    while(n!=0){
          if((n&1)==1)
          s='1'+s;
          else
          s='0'+s;
          n=n>>1;
           }
           cout<<"s:"<<s<<endl;
    cin.get();
    }

- Jerry June 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

Hi Everyone , converting numbers after decimal point is different from before decimal point. After decimal point we have to multiply each digit with 2 and is result is greater than or equal to 1, then its 1 else its 0.

- Prashanth June 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include<cmath>
using namespace std;
void binary(int n)         //prints binary representation of number before decimal point
{
	if(n>1)
	{
		binary(n/2);
	}
	cout<<n%2;
}

void bin_dec(double n)    // prints binary representation of number after decimal point
{
    for(int i=0;i<4;i++)	
	{
		n=n*2;
    	if(n>=1)
    	{
	    	cout<<1;
	    	n=n-floor(n);
	    	continue;
    	}
    	cout<<0;
    }
}

int main()
{
	double n,n2;
	int n1;
	cout<<"Enter the decimal number. \n";
	cin>>n;
	n1=floor(n);
	binary(n1);
	n2=n-n1;
	cout<<".";
	bin_dec(n2);
	return 0;
}

- chaitanya June 25, 2013 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More