Amazon Interview Question
SDE1sTeam: Marketplace
Country: United States
Interview Type: Written Test
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)
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);
}
}
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;
}
{{
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;
}
}}
#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;
}
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());
}
}
}
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");
}
}}
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");
}
}}
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);
}
}
}
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);
}
}
}
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);
}
}
}
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);
}
}
}
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);
}
}
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);
}
}
}
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);
}
}
}
- Joe May 03, 2017