• 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

TicTacToe Game As Asked in Flipkart

Max Sum in circularly situated Values

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

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

System Design: Designing a LLD for Hotel Booking

Memory Efficient LinkedList

Templates in C++

1014 Practice Question of New GRE – Princeton

Naurki.com Security Breach

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

VMWare Openings

Leetcode: Merge Intervals

Maximum path sum between two leaves

Reversal of LinkedList

VMWare SDEII Interview

SAP Hiring Off-Campus General Aptitude

Introduction To Number Theory ( Part 1 )

Maximum difference between two elements s.t larger element appears after the smaller number

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

Closed Parentheses checker

Maximum size of square sub matrix with all 1’s in a binary matrix

Find the element that appears once others appears thrice

Printing Longest Common Subsequence

Find next greater number with same set of digits

Common Ancestor in a Binary Tree or Binary Search Tree

Convert Decimal to Roman numbers / Romanizer HackerEarth Code

Diagonal Traversal of Binary Tree

Stickler thief

Find Pythagorean Triplets in an array in O(N)

Flipkart SDET Interview Experience

Copyright © 2025 · Genesis Framework · WordPress · Log in