• 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

Python String and numbers

Maximum sum contiguous subarray of an Array

Find if two rectangles overlap

Find next greater number with same set of digits

simple sql injection

Minimum insertions to form a palindrome

SAP Off Campus Hiring_ March 2015 Sample Questions

Stock Buy Sell to Maximize Profit

25 horses 5 tracks Find 3 fastest puzzle

Right view of Binary tree

Find shortest distances between every pair of vertices ( Dynamic Programming Floyd Warshall Algorithm)

Client Server C program

Flipkart SDET Interview Experience

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

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

System Design: Designing a LLD for Hotel Booking

ADOBE Aptitude C Language Test

Add Sub Multiply very large number stored as string

SAP Off Campus Hiring_ March 2015 Computer Skills

Find two non repeating elements in an array of repeating elements

flattens 2 D linked list to a single sorted link list

Reversal of LinkedList

Urban Ladder Written Test.

Linked List V/S Binary Search Tree

Printing Longest Common Subsequence

Search element in a matrix with all rows and columns in sorted order

Microsoft BING Interview Experience

Print all nodes that are at distance k from a leaf node

Maximum of all subarrays of size k

Python List

Copyright © 2025 · Genesis Framework · WordPress · Log in