Amazon Interview Question


Country: India




Comment hidden because of low score. Click to expand.
2
of 4 vote

@Nascent

Do we need to compute the two's compliment form and print it out as a string? Otherwise, since a computer anyway stores a number in two's compliment form, we can just print out the bits of the memory word.

- Murali Mohan July 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 votes

isn't two compliement of a negative number is the postive number itself.

int main()
{
int x=-1;
printf("%x\n", x);
printf("%x\n", ~x+1);
}

- aka July 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes, your code seems correct

- Sairam Yendluri July 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

2's complement of (-x) is (x) itself as said by everybody.
Just multiply the number by (-1) or do (~x + 1) to get the result.

- NaMo July 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I guess the logic is just flip all the digits and add 1 to the result

- Phoenix July 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

2's complement of a negative number is simply a positive magnitude of that number. For example, if number is -6 its 2's complement will be 6.
I think this is the code

#include <stdio.h>

main() {
	int a=-6;
	a = (~a)+1;
	printf("%d\n",a);
}

- Sairam Yendluri July 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

it can also be
no = no * -1;
or
no = -(no)
:)

- Phoenix July 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

:) yes ofcourse

- Sairam Yendluri July 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class TwosComplement {
	public static void main(String[] args) {
		int no = -8;
		printBits(no);
		printBits(~no);
		printBits(~no + 1);
	}

	public static void printBits(int no) {
		System.out.println("\nBinary format of:" + no + "\n"
				+ Integer.toBinaryString(no));
		for (int i = 31; i >= 0; i--)
			System.out.print((((no & (1 << i)) >>> i) > 0) ? "1" : "0");
	}
}

- Phoenix July 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I would suggest that there be more clarity on the input. One possible cpp solution.

#include<iostream>
#include<stdlib.h>
#include<cstring>
using namespace std;
//assumptions:
//1. input is a binary number in a char string
int main(void)
{
	string str;
	cin >> str;
	//flip the digits	
	cout << str << endl;
	for (int i=0;i<str.size();i++)
		str[i] = ((str[i] == '0') ? '1' : '0');
	cout << str <<endl;
	for(int i = str.size();i>=0;i--)
		if (str[i] == '0')
		{
			str[i] = '1';
			break;
		}
		else
		{
			str[i] = '0';
			continue;
		}
	cout << str <<endl;
	return 0;
}

- abOriginal July 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;

int main() {
    int n;
    while(cin>>n){
        cout<<n<<" : "<<(~n)+1<<endl;
    }
    return 0;
}

- Eduardo Cruz July 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
main(){

int a,b,k;

printf("enter the value");
scanf("%d",&a);

if(a>0){
k=~a+1;
printf(" \n %d 2's complement %d",a,k);
}
else{
k=0xff;
b=k^a;
printf("\n %d 2's complement %d",a,b);
}



}

- Navdeep Gupta July 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main(){
    int a = -95;
    printf("%d",(255^abs(a))+1);
    getch();
}

- anonymous July 03, 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