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


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