Amazon Interview Question for SDE-3s

Country: United States
Interview Type: In-Person

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

``````package main

import "fmt"

func Sum(a, b int) int {
res := a ^ b
y := a & b << 1
for y > 0 {
a = res
res = res ^ y
y = a & y << 1
}
return res
}

func main() {
fmt.Println(Sum(10, 20)) // Output: 30
}``````

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

I would just write the simple logic here :

``````int Add(int x, int y){
while(y!=0){
int carry = (x&y)<<1;
x=x^y;
y=carry;
}
return x;
}``````

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

handles all cases easy ans simple

``````#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b;
cin>>a>>b;
while(b!=0)
{
int carry= a&b;
a = a ^ b;
b = carry<<1;
}
cout<< a<<endl;;
}``````

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

#include<stdio.h>

{
// Iterate till there is no carry
while (y != 0)
{
// carry now contains common set bits of x and y
int carry = x & y;

// Sum of bits of x and y where at least one of the bits is not set
x = x ^ y;

// Carry is shifted by one so that adding it to x gives the required sum
y = carry << 1;
}
return x;
}

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.