• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Generate next palindrome number

September 17, 2014 by Dhaval Dave

Several Companies like MS, Amazon and Flipkart has started asking this question to Generate next palindrome number.

Say K is given, Generate N such that N is palindrome and N > K.

Logic 1)  Create a Function which checks a number is palindrome or not.

while(1)
{
   flag = checkPali(++k);
   if (flag ==1) break;
}

Logic 2)

Generate number it self from given number.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
    char a[1000002];
    int test,i,flag,check,l,m,k;
    scanf("%dn",&test);
    while(test--)
    {
        flag=1;
        gets(a);
        l=strlen(a);
        // baseof(length / 2) in integer.
        k=l>>1; 
        check=0;
        for(i=0;i<l;i++)
        {
            if(a[i]!='9')
            {
                flag=0;
                break;
            }
        }
        //handling case when 9, 99, 999 is given.(only 9s)
        if(flag==1)
        {
            a[0]='1';
            for(i=1;i<l;i++)
                a[i]='0';
            a[l]='1';
            a[l+1]='';
            check=1; //to check palindrome or not number
        }
        
        flag=0;
        if(check!=1)
        {
            for(i=0;i<k;i++)
            {
                if(a[i]<a[l-1-i]){
                flag=-1;
                }
                else if(a[i]>a[l-1-i]){
                flag=1;}
                a[l-1-i]=a[i];
            }

            if(!(l&1))
            m=k-1;
            else m=k;
            if(flag==0||flag==-1)
            {
                i=0;
                while(a[m-i]=='9')
                {
                    a[m-i]='0';
                    a[l-1-m+i]='0';
                    i++;
                }

                a[m-i]++;
                a[l-1-m+i]=a[m-i];
        }
    }
    printf("%sn",a);
    }
    return 0;
}

 

Here is sample code. http://ideone.com/pfGBf9

Similar Articles

Filed Under: Adobe Interview Questions, Amazon Interview Question, Flipkart Interview Questions, Microsoft Interview Questions, problem Tagged With: Array

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

robot standing at first cell of an M*N matrix. It can move only in two directions, right and down. In how many ways, it can reach to the last cell i.e. (M, N) Code it

Print vertical sum of all the axis in the given binary tree

In Given LinkedList Divide LL in N Sub parts and delete first K nodes of each part

Wrong Directions given find minimum moves so that he can reach to the destination

Find the element that appears once others appears thrice

Level order traversal in Spiral form

Check if an array has duplicate numbers in O(n) time and O(1) space

Binary Tree Isomorphic to each other

There are N nuts and N bolts, u have to find all the pairs of nuts and bolts in minimum no. of iteration

Find position of the only set bit

Length of the longest substring without repeating characters

Given a float number convert it into the string WITHOUT using any inbuilt Function

Binary Tree in Java

Python Array String

Trapping Rain Water

LeetCode: Binary Tree Maximum Path Sum

Closed Parentheses checker

Number of Islands BFS/DFS

Find next greater number with same set of digits

Puzzle : 100 doors in a row Visit and Toggle the door. What state the door will be after nth pass ?

Handle duplicates in Binary Search Tree

25 horses 5 tracks Find 3 fastest puzzle

Convert Decimal to Roman numbers / Romanizer HackerEarth Code

Printing intermediate Integers between one element & next element of array

strtok()

Calculate price of parking from parking start end time prices

Find min element in Sorted Rotated Array (With Duplicates)

Find if a binary tree is height balanced ?

VMWare SDEII Interview

K’th Largest Element in BST when modification to BST is not allowed

Copyright © 2025 · Genesis Framework · WordPress · Log in