Amazon Interview Question for SDE-2s


Country: United States




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

Below method should work:

public static int mulipleCustomLooping(int a, int b) {

int getresultat = 0;
for (int i = 0; i < a; i++) {
getresultat += b;
}

return getresultat;
}

- Yogesh shukla July 23, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I do not think that is a bitwise operation.

- Kevin August 17, 2020 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int mulipleCustomLooping(int a, int b) {
int getresultat = 0;
for (int i = 0; i < a; i++) {
getresultat += b;
}
return getresultat;
}

- Yogesh Shukla July 23, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int multiply(int i1, int i2) {
        boolean isPositive = false;

        if(i1==0 || i2==0) return 0;

        if(i1<0 & i2<0) isPositive=true;
        else if(i1>0 & i2>0) isPositive=true;

        if(i1<0) i1 = ~i1+1;
        if(i2<0) i2 = ~i2+1;

        int acc = i1;

        for(int i=2; i <= i2; i++) acc = add(acc, i1);
        return isPositive ? acc : ~acc+1;
    }

public static int add(int i1, int i2){
        final int mask = 1;

        int subsum=0, result=0, pos=0;
        boolean carryOver=false;

        while(i1>=0 || i2>=0){
            final int digitA = i1 & mask, digitB = i2 & mask;

            /*
                0 ^ 0 = 0
                0 ^ 1 = 1
                1 ^ 0 = 1
                1 ^ 1 = 0
             */

            subsum = digitA ^ digitB;

            if(carryOver) subsum ^= 1;

            carryOver = (carryOver && (digitA == 1 || digitB == 1)) |
                    (!carryOver && digitA == digitB && digitB == 1);

            i1 >>= 1; i2 >>= 1;
            subsum <<= pos;
            pos++;
            result |= subsum;

            if(i1 == i2 && i2 == 0 && !carryOver) break;;
        }

        return result;
    }

- Yev August 26, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
i1 * i2
int result = 0;
add i1 to result i2 times.
*/

public int multiple (int i1, int i2) {
 int i = 0;
 int result = 0;
 while (i2 > i) {
  result = add(result, i1);
  i = add(i, 1);
 }
 return result;
}

public int add(int i1, int i2) {
 // iterate until there is no carry
 while (i2 != 0) {
  // carry contains common set bits of i1 and i2
  int carry = i1 & i2;
  // sum of bits of i1 and i2 where at least either of i1 and i2 bits is 0
  i1 = i1 ^ i2;
  // carry is shifted by one so that adding it to i1 gives the required sum
  i2 = carry << 1;
 }
 return i1;
}

- Euihoon Seol October 18, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is my approach -:

int ans = 0;
     for(i=0;i<a;i++)
     {
           ans = ans xor b + (ans && b) << 1;
     }

- sushocoder November 25, 2020 | 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