• 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

Password Predictor

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

Get Minimum element in O(1) from input numbers or Stack

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

Skiing on Mountains Matrix

Top 10 Interviews Techniqes for Campus Interview in IIT NIT BITS for MTech

Find if a binary tree is height balanced ?

Inorder and Preorder traversals of a Binary Tree given. Output the Postorder traversal of it.

Convert Decimal to Roman numbers / Romanizer HackerEarth Code

Adobe Interview Questions 8 month Exp

1014 Practice Question of New GRE – Princeton

CodeChef’ RRCOPY

Circular Linked List

Stickler thief

Python List

Memory Efficient LinkedList

Convert number to words java

Reverse a Linked List in groups of given size

strtok()

Find the smallest window in a string containing all characters of another string

Find an index i such that Arr [i] = i in array of n distinct integers sorted in ascending order.

Sort an array according to the order defined by another array

‘N’ Story Building, with 1,2,3 steps how many ways can a person reach top of building.

Print Power Set of a Set

Count Possible Decodings of a given Digit Sequence

Find the kth number with prime factors 3, 5 and 7

Cisco Hiring Event 21st – 22nd Feb 2015

Linked List V/S Binary Search Tree

Given Set of words or A String find whether chain is possible from these words or not

Copyright © 2025 · Genesis Framework · WordPress · Log in