• 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

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

SAP Hiring Off-Campus General Aptitude

Generate largest number arranging a no. of given non negative integer numbers

Amazon Interview On-Campus For Internship – 1

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

LeetCode : Word Search

Check a String is SUBSEQUENCE of another String Find Minimum length for that ( DNA Matching )

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

LeetCode: Container With Most Water

Client Server C program

Add Sub Multiply very large number stored as string

Connect n ropes with minimum cost

Templates in C++

Find the element that appears once others appears thrice

building with N steps, we can take 1,2,3 steps calculate number of ways to reach at top of building

Circular Linked List

Walmart Labs Interview Experience

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

Flipkart SDET Interview Experience

Microsoft BING Interview Experience

Rectangular chocolate bar Create at least one piece which consists of exactly nTiles tiles

DFS (Depth First Search)

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

Find if a binary tree is height balanced ?

Find Pythagorean Triplets in an array in O(N)

SAP Off Campus Hiring_ March 2015 Sample Questions

Urban Ladder Written Test.

Leetcode: Merge Intervals

LeetCode: Binary Tree Maximum Path Sum

Diagonal Traversal of Binary Tree

Copyright © 2025 · Genesis Framework · WordPress · Log in