PuzzlersWorld.com

  • Hacker Puzzle
  • Interview Puzzles
  • Number Puzzles
  • Maths Puzzles

implement isPalindrome(int n)

(2 votes, average: 1.00 out of 5)

June 27, 2013 by puzzler 2 Comments

Problem Statement

Implement the function boolean isPalindrome (int n);

Which will return true if the bit-wise representation of the integer 
is a palindrome and false otherwise.


Motivation: This is a favorite interview question of Amazon guys.

Solution:
The idea is simple, we take two pointers one at leftmost bit and another at rightmost bit, extract bits information using the pointers both bits should either be 0 or 1. and move the pointers until they meet.
Now how would you extract the bit is tricky one, basically you first get number of bits in the integer and then shift 1 left by (number of bits -1) this will become our leftmost pointer and number 1 will be our rightmost pointer. Use & operator between original number and these pointers if the result is 0 then the corresponding bit is zero else(non-zero) then corresponding bit is 1.
Note: check for non-zero rather then > 0 as 1000000000000 will be negative.

Here is the java code to get the size of the integer

	public int getSizeOfInteger(){
		int size =1;
		int num =1;
		//shift the number untill it becomes negative(1 followed by 0's)
		while(num > 0){
			num = num<<1;
			size++;
		}
		return size;
	}

Here is the java code to get if the bit representation of the number is palindrome or not

	public boolean isPalindrome(int n){
		int rightPointer =1;
		int leftPointer = 1<<(getSizeOfInteger()-1);
		
		int count = getSizeOfInteger()/2;
		while(count > 0){
			//Checking if both bits are 0 or both are non-zero and then negate
			if( !(((n & leftPointer) != 0 && (n & rightPointer) != 0) ||
					( (n & leftPointer) == 0 && (n & rightPointer) == 0))){
				return false;//Both bits are not same
			}
			count--;
		}
		return true;
	}
  • Share on Whatsapp
  • Share on Facebook
  • Share on Twitter
Facebook Comments
Next Puzzle
DS with insert, delete and getRandomElement in O(1)

Checkout more Interview Questions Tags: Algorithm, Amazon Interview Questions, medium, Solved Puzzles

Comments

  1. Shilpa says

    August 18, 2013 at 10:03 am

    can we do this que as..take the reverse of the no n dn compare if the reverse n original no are equal , then its a palindrome otherwise not…

    Reply
    • puzzlersworld says

      August 18, 2013 at 12:21 pm

      Yes that will also be correct, but it will be higher in complexity and space.

      Reply

Leave a Comment Cancel reply

Submit your Puzzle

You may also like

  • Magic Trick Solution – Google Code Jam 2014
  • Cake Cutting solution: Facebook hacker cup 2013 Round 2
  • Dead Pixels solution: Facebook hacker cup 2013 Round 1
  • Objective Questions set C/C++ – Part 3
  • RoboElection solution: Facebook hacker cup 2013 Round 2
  • Full Binary Tree Solution – Google Code Jam
  • DS with insert, delete and getRandomElement in O(1)
  • Swap two integer variables
  • Fair and Square Solution: Google codejam 2013 Qual Round
  • Sort the array in O(N)

Categories

  • Aive hi Puzzles
  • Akbar and Birbal
  • Alphabetical Puzzles
  • Bollywood Puzzles
  • Google Code jam
  • Hindi Riddles
  • Interview Puzzles
  • Interview Questions
  • Logical Puzzles
  • Malayalam Puzzles
  • Maths Puzzles
  • Miscellaneous
  • Number Puzzles
  • Picture Puzzles
  • Riddles
  • Tamil Puzzles
  • Technical

Social

  • View puzzlersworld’s profile on Twitter
privacy policy

Copyright © 2025 · eleven40 Pro Theme on Genesis Framework · WordPress · Log in

  • Hacker Puzzle
  • Logo Puzzles
  • Optical Illusions
  • WhatsApp Puzzles
  • Picture Puzzles
  • Riddles
    ▼
    • Hindi Riddles
  • Bollywood Puzzles
  • Alphabetical Puzzles
  • Aive hi Puzzles
  • Interview Puzzles
  • Logical Puzzles
  • Interview Questions
    ▲
    • Data Structures
    • Binary Tree
    • Algorithms
    • Recursion Questions
    • Amazon Interview Questions
    • Snapdeal Interview Questions
    • Google Code jam
  • Technical
  • Akbar and Birbal
  • Number Puzzles
  • Maths Puzzles
  • Miscellaneous