Amazon Interview Question for SDE1s


Team: Marketplace
Country: United States
Interview Type: Written Test




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

public static boolean checkPalindrome(int j){
    String s = Integer.toBinaryString(j);
    for(int i=0;i<s.length()/2;i++){
      if(s.charAt(i)!=s.charAt(s.length()-i-1))
          return false;
    }
    return true;
  }

- Joe May 03, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Brute force:

Length of binary representation of a decimal n = floor(log(n, 2)) + 1. Let's call this k

Iterate from i <- 0 to floor(k / 2):
	if the bit at i is not equal to the bit at k - i - 1:
		return False
return True

We can check if the bit at i is not equal to the bit at k - i - 1 by:
mask = 1 << k - i - 1
if !((mask & n == mask) or (mask & n == 0))
(the bits are equal only if both are 1, in which case the first condition is true,
or both are none in which case the second condition is true)

- horvthpeter May 03, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public static boolean checkPalindrome(int j){
    String s = Integer.toBinaryString(j);
    for(int i=0;i<s.length()/2;i++){
      if(s.charAt(i)!=s.charAt(s.length()-i-1))
          return false;
    }
    return true;
  }

- Joe May 03, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

In Java:

public class PalindromicBits {

	public static void main(String[] args) {
		checkPalindromBits(9);
		checkPalindromBits(8);
		checkPalindromBits(12);
		checkPalindromBits(16);
		checkPalindromBits(32);
		checkPalindromBits(33);
	}
	
	public static void checkPalindromBits(int num) {
		String binaryString = convertToBinaryString(num);
		if (isPalindrome(binaryString.toCharArray())) {
			System.out.println("palindrome");
		} else {
			System.out.println("not palindrome");
		}
	}
	
	public static String convertToBinaryString(int n) {
		StringBuilder sb = new StringBuilder();

		 while(n > 0) {
		  if ((n & 1) == 1) {
		   sb.append("1");
		  } else {
		   sb.append("0");
		  }

		  n = n >> 1;
		 }

		 return sb.reverse().toString();
	}

	public static boolean isPalindrome(char[] c) {	 
		int start = 0;
	    int end = c.length - 1;
	    int mid = (start + end)/2;
	    int i;
	    for (i = start; i <= mid; i++) {
	      if (c[start] == c[end]) {
	        start++;
	        end--;
	      }
	      else {
	        break;
	      }
	    }
	    return (i == mid + 1); 			
	}
}

- guilhebl May 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

@Adriano your solution is wrong take 1101 for example

- horvthpeter May 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

generate the reverse bit-pattern via left shift and right shift. Then compare the result with the original.

bool isPalindrome(int num)
{
    int reverse = 0;
    int temp = num;
    while(temp)
    {
        reverse = (reverse << 1) || (temp & 1);
        temp >>= 1;
    }
    return (reverse == num); 
}

- AnonyMouse May 05, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool is_palindrome(const int num){
return !(num % 2 == 0);
}

- Adriano May 03, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool is_palindrome(const int num){
    return !(num % 2 == 0);

}

- Adriano May 03, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Check bits from LSB and MSB and compare. If they are equal, decrement MSB and increment LSB and check further.

		boolean getBit(int num, int i)
		{
			//(i-1) as bit indexes start at zero
			return ( num & (1<<(i-1)) ) != 0;
		}

		boolean isPalindrome(int num)
		{
			int l = 1;
			//Depending upon byte/nibble length, value of r could vary.
			//(9 is represented as 1001 in nibble, in 32-bit integer it'll be represented as 28 zeros followed by 1001)
			//1001 looks palindrome but in 32-bit representation it is not.
			//so for that reason it'll fail for 9. Here I have taken it to be 32-bits.
			int r = Integer.SIZE;
			
			
			while( l < r )
			{
				if(getBit(num, l) != getBit(num, r))
					return false;
				
				r--;
				l++;
			}
			
			return true;
		}

- Anonymous May 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{{

int isP(uint n) {
int i = 0;

for (i = 0; n >= (1 << i); i++);

if (i % 2 == 0) {
int factor = (i >> 1);
int mostSbytes = (n >> factor);
int leastSBytes = (n ^ (mostSbytes << factor));
return mostSbytes == leastSBytes;
}

int factor = ((i + 1) >> 1);
int mostSbytes = (n >> factor);
int leastSBytes = (n ^ (mostSbytes << factor));
return mostSbytes == leastSBytes;
}

}}

- NunuM May 05, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>

bool isBinaryPal(int j){
  unsigned char iSize = sizeof(j)*8;
  unsigned char msbi = iSize - __builtin_clz(j) - 1;
  for(unsigned char i = 0; i < (msbi - i); i++){
    if(((j & (1 << (msbi - i))) >> (msbi - i))
    ^ ((j & (1<<i))>>i)){
      return false;
    }
  }
  return true;
}

int main(){
  std::cout<<isBinaryPal(2)<<std::endl;
  return 0;

}

- D May 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class BinayPalandrom {
public static void main(String args[]) {
int givenN = 27;
int n = givenN;
StringBuilder string = new StringBuilder();
StringBuilder stringReversal = new StringBuilder();
while (n != 1) {
int j = n % 2;
n = n / 2;
string.append( j);
stringReversal.insert(0, j);
}
string.append(1);
stringReversal.insert(0, 1);
if(string.toString().equalsIgnoreCase(stringReversal.toString())){
System.out.println("Its Palendrom :"+stringReversal.toString());
}else{
System.out.println("Its Not Palendrom :"+stringReversal.toString());
}

}
}

- Anonymous May 15, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Scanner;
class Inttobin
{
public static void main(String a[])
{int bin[]=new int[10];
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer: ");
int n = scanner.nextInt();

int i=0;
while(n>0)
{bin[i++]=n%2;
n=n/2;
}
for (int j=i-1;j>=0;j--)
{
System.out.print(bin[j]);

}
for(int j=0;j<i/2;j++){
if(bin[j]!=bin[i-j-1])
System.out.println("no");
else
System.out.println("yes");

}
}}

- Anonymous June 02, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Scanner;
class Inttobin
{
public static void main(String a[])
{int bin[]=new int[10];
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer: ");
int n = scanner.nextInt();

int i=0;
while(n>0)
{bin[i++]=n%2;
n=n/2;
}
for (int j=i-1;j>=0;j--)
{
System.out.print(bin[j]);

}
for(int j=0;j<i/2;j++){
if(bin[j]!=bin[i-j-1])
System.out.println("no");
else
System.out.println("yes");

}
}}

- sssatishkumar96 June 02, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def check_palindrome(num):
    s = bin(num)[2:]
    return s == s[::-1]

- hasan.iqbal.anik October 23, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class BinaryRepresentationPalindrome {

	public static void isPalindrome(int num) {
		String in = Integer.toBinaryString(num);
		char[] charArray = in.toCharArray();
		boolean isPalindrome = true;
		for (int i = 0, j = charArray.length - 1; i < charArray.length / 2; i++, j--) {
			if (charArray[i] == charArray[j]) {
				continue;
			}
			isPalindrome = false;
			break;
		}
		System.out.println(in + " is palindrome = " + isPalindrome);
	}

	public static void main(String[] args) {
		for(int i=1;i<=16;i++) {
			isPalindrome(i);
		}
	}
}

- Krishna Murthy P Mirajkar June 23, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class BinaryRepresentationPalindrome {

	public static void isPalindrome(int num) {
		String in = Integer.toBinaryString(num);
		char[] charArray = in.toCharArray();
		boolean isPalindrome = true;
		for (int i = 0, j = charArray.length - 1; i < charArray.length / 2; i++, j--) {
			if (charArray[i] == charArray[j]) {
				continue;
			}
			isPalindrome = false;
			break;
		}
		System.out.println(in + " is palindrome = " + isPalindrome);
	}

	public static void main(String[] args) {
		for(int i=1;i<=16;i++) {
			isPalindrome(i);
		}
	}
}

- Krishna Murthy P Mirajkar June 23, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class BinaryRepresentationPalindrome {

	public static void isPalindrome(int num) {
		String in = Integer.toBinaryString(num);
		char[] charArray = in.toCharArray();
		boolean isPalindrome = true;
		for (int i = 0, j = charArray.length - 1; i < charArray.length / 2; i++, j--) {
			if (charArray[i] == charArray[j]) {
				continue;
			}
			isPalindrome = false;
			break;
		}
		System.out.println(in + " is palindrome = " + isPalindrome);
	}

	public static void main(String[] args) {
		for(int i=1;i<=16;i++) {
			isPalindrome(i);
		}
	}

}

- Krishna Murthy P Mirajkar June 23, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class BinaryRepresentationPalindrome {

public static void isPalindrome(int num) {
String in = Integer.toBinaryString(num);
char[] charArray = in.toCharArray();
boolean isPalindrome = true;
for (int i = 0, j = charArray.length - 1; i < charArray.length / 2; i++, j--) {
if (charArray[i] == charArray[j]) {
continue;
}
isPalindrome = false;
break;
}
System.out.println(in + " is palindrome = " + isPalindrome);
}

public static void main(String[] args) {
for(int i=1;i<=16;i++) {
isPalindrome(i);
}
}
}

- Krishna Murthy P Mirajkar June 23, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
{{{public class BinaryRepresentationPalindrome { public static void isPalindrome(int num) { String in = Integer.toBinaryString(num); char[] charArray = in.toCharArray(); boolean isPalindrome = true; for (int i = 0, j = charArray.length - 1; i < charArray.length / 2; i++, j--) { if (charArray[i] == charArray[j]) { continue; } isPalindrome = false; break; } System.out.println(in + " is palindrome = " + isPalindrome); } public static void main(String[] args) { for(int i=1;i<=16;i++) { isPalindrome(i); } } } }} - Krishna Murthy P Mirajkar June 23, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class BinaryRepresentationPalindrome {

	public static void isPalindrome(int num) {
		String in = Integer.toBinaryString(num);
		char[] charArray = in.toCharArray();
		boolean isPalindrome = true;
		for (int i = 0, j = charArray.length - 1; i < charArray.length / 2; i++, j--) {
			if (charArray[i] == charArray[j]) {
				continue;
			}
			isPalindrome = false;
			break;
		}
		System.out.println(in + " is palindrome = " + isPalindrome);
	}

	public static void main(String[] args) {
		for(int i=1;i<=16;i++) {
			isPalindrome(i);
		}
	}

- Krishna Murthy P Mirajkar June 23, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class BinaryRepresentationPalindrome {
public static void isPalindrome(int num) {
String in = Integer.toBinaryString(num);
char[] charArray = in.toCharArray();
boolean isPalindrome = true;
for (int i = 0, j = charArray.length - 1; i < charArray.length / 2; i++, j--) {
if (charArray[i] == charArray[j]) {
continue;
}
isPalindrome = false;
break;
}
System.out.println(in + " is palindrome = " + isPalindrome);
}

public static void main(String[] args) {
for(int i=1;i<=16;i++) {
isPalindrome(i);
}
}
}

- Krishna Murthy P Mirajkar June 23, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class BinaryRepresentationPalindrome {

	public static void isPalindrome(int num) {
		String in = Integer.toBinaryString(num);
		char[] charArray = in.toCharArray();
		boolean isPalindrome = true;
		for (int i = 0, j = charArray.length - 1; i < charArray.length / 2; i++, j--) {
			if (charArray[i] == charArray[j]) {
				continue;
			}
			isPalindrome = false;
			break;
		}
		System.out.println(in + " is palindrome = " + isPalindrome);
	}

	public static void main(String[] args) {
		for(int i=1;i<=16;i++) {
			isPalindrome(i);
		}
	}
}

- Krishna Murthy P Mirajkar June 23, 2018 | 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