Question: Given an Integer, you need to determine if it is a palindrome or not. You should not use any extra space in the process.
    Input: 121
    Output: Palindrome

    At the first go, the problems seems very easy to solve. We just need to reverse the number and check if it remains the same. Right? Don’t be deceived by this problem which seems too easy. Also note the restriction of doing it without extra space. We also need to think of a generic solution that is not language/platform specific.

    First, the problem statement did not specify if negative integers qualify as palindromes. Does negative integer such as -1 qualify as a palindrome? Finding out the full requirements of a problem before coding is what every programmer must do. For the purpose of discussion here, we define negative integers as non-palindromes.

    The most intuitive approach is to first represent the integer as a string, since it is more convenient to manipulate. Although this certainly does work, it violates the restriction of not using extra space. (ie, you have to allocate n characters to store the reversed integer as string, where n is the maximum number of digits). I know, this sound like an unreasonable requirement (since it uses so little space), but don’t most interview problems have such requirements?

    Another approach is to first reverse the number. If the number is the same as its reversed, then it must be a palindrome. You could reverse a number by doing the following:

    int reverse(int num)
    {
        assert(num >= 0);   // for non-negative integers only.
        int rev = 0;
        while (num != 0)
        {
            rev = rev * 10 + num % 10;
            num /= 10;
        }
        return rev;
    }
    

    This seemed to work too, but did you consider the possibility that the reversed number might overflow? If it overflows, the behavior is language specific (For Java the number wraps around on overflow, but in C/C++ its behavior is undefined). Yuck.

    Of course, we could avoid overflow by storing and returning a type that has larger size than int (ie, long long). However, do note that this is language specific, and the larger type might not always be available on all languages.

    We could construct a better and more generic solution. One pointer is that, we must start comparing the digits somewhere. And you know there could only be two ways, either expand from the middle or compare from the two ends.

    It turns out that comparing from the two ends is easier. First, compare the first and last digit. If they are not the same, it must not be a palindrome. If they are the same, chop off one digit from both ends and continue until you have no digits left, which you conclude that it must be a palindrome.

    Now, getting and chopping the last digit is easy. However, getting and chopping the first digit in a generic way requires some thought. The solution below takes care of it.

    int isIntPalindrome(int x)
    {
        if (x < 0)
        return 0;
        int div = 1;
        while (x / div >= 10)
        {
            div *= 10;
        }
    
        while (x != 0)
        {
            int l = x / div;
            int r = x % 10;
            if (l != r)
                return 0;
            x = (x % div) / 10;
            div /= 100;
        }
        return 1;
    }
    
    6 comments
    0 FacebookTwitterLinkedinWhatsappEmail
  • Here we will discuss the easiest and most simple way to change case of alphabets. Hint : We will be using bit hacks. Often during our programming needs we need to change the case of alphabets. This can be a problem requirement or simply our need to solve the question. There are several ways to do it, but what can be more beautiful than using bit hacks for the same. We discusses some of the BIT Hacks in these posts:- Low Level Bit Hacks Some advanced Bit Hacks Here I…

    0 FacebookTwitterLinkedinWhatsappEmail
  • MiscTheory

    Advanced level Bit Hacks

    by nikoo28
    10 minutes read

    Here we shall discuss some high level Bit hacks that if used cleverly can really speed up your program execution time. We discussed some of the basic hacks in this post:- Low level bit hacks you must know. Please go through the post to get a little understanding of how the things work. Smart learners are anyways welcome to proceed. ;-) BIT HACK 6: Turn off the rightmost 1-bit. y = x & (x-1) Now it finally gets more interesting!!! Bit hacks #1 – #5 were kind of boring to…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Theory

    Low level Bit Hacks that you must know

    by nikoo28
    8 minutes read

    Here we shall discuss some very low level Bit hacks that can really speed up your program execution time when used effectively. Every now and then we see bit level operators. Bit hacks are ingenious little programming tricks that manipulate integers in a smart and efficient manner. Instead of performing some operation (such as counting the 1 bits in an integer) by looping over individual bits, these programming nuggets do the same with one or two carefully chosen bitwise operations. To get things going I’ll assume that you know what…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: You are given a function rand7() – that generates random numbers from 1-7. Write a function rand10() – that uses rand7() to generate random numbers from 1-10. This appear to be one of those probabilistic analysis questions. You should be familiar with the concept of expected value, as it could be extremely helpful in probabilistic analysis. Hint: Assume you could generate a random integer in the range 1 to 49. How would you generate a random integer in the range of 1 to 10? What would you do if…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Arrays

    An array puzzle

    by nikoo28
    3 minutes read

    Question: There is an array A[N] of N numbers. You have to compose an array Output[N] such that Output[i] will be equal to multiplication of all the elements of A[N] except A[i]. Solve it without division operator and in O(n). For example Output[0] will be multiplication of A[1] to A[N-1] and Output[1] will be multiplication of A[0] and from A[2] to A[N-1]. Array 1: {4, 3, 2, 1, 2} Output: {12, 16, 24, 48, 24} Since the complexity required is O(n), the obvious O(n2) brute force solution is not good…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Theory

    Playing with Pointers

    by nikoo28
    5 minutes read

    Question: What is a Pointer? What are its limitations? What are its benefits? How do we use it? What all operation we can perform using it? In this article we are going to discover answers to all these questions. Pointer is a variable that contain the address of another variable and that variable can be a structure, array or basic data type. The basic syntax to define the pointer is as follows: Datatype * identifier; Datatype is the data type of the variable whose address we are going to store…

    0 FacebookTwitterLinkedinWhatsappEmail

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More