IBM Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
1
of 1 vote

I have the following algo: please let me know if there are any faults in it..

Case I: The given number is a palindrome..eg: 1234321
then simply increment the center digit by 1 i.e the ans is 1235321

CaseII: The given number is not a palindrome:
Subcase 1: The number to the left of the middle digit is greater than the number on the right eg:2194135 (here 219 > 135)
reverse the left side numbers and replace the right side numbers with them, therefore the ans will be 2194912
Subcase 2:The number to the right of the middle digit is greater than the number on the left eg:1354219
then increment the middle digit by 1 and then reverse the left side numbers and replace the right side numbers with them.. therefore the answer would be 135 5(middle digit incremented) 531(reversed).1355531.

Note: in case the middle digit is 9 then incrementing it would make it 10..so put 0 instead of 9 and increment the first right and left neighboring digit of 9 by 1.

- Nameless September 18, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I dont think this answer is right, why would you want to increase the middle digit by 1? the answer should be 1354531.

- qianduixue July 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

also, for subcase 1, if the number is 8754719, then you answer would be 8754578, which is not correct.
My algorithm is as follows:
Case I, same;
Case II,
subcase I, if The reverse of "number to the left of the middle digit" is greater than the number on the right eg:2194135 (here 912 > 135)
reverse the left side numbers and replace the right side numbers with them, therefore the ans will be 2194912

subcase II, if The reverse of "number to the left of the middle digit" A is smaller than the number on the rightB eg:2174835 (here A = 712 < B = 835), then find the index i of first number in A that is less than Bi, set Ai = Bi and set every digit else in B equals to A

- Anonymous July 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

//#include<conio.h>
#include<stdio.h>
void main()
{
int x,y,z;
printf("plz enter the number.");
scanf("%d",&x);
if(check(++x))
print("%d",x);
getch();
}
int check(int i)
{ int j,k;
i=j;
while(j<10)
{
k=j%10;
k=k*10;
j=j/10;
k=k+j;
}
if(i==k)
return 1;
else return 0;
}

- mukul jain August 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can someone explain the algo?

- Anonymous August 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<string.h>
int main()
{
	char a[1000001];
	int n;
	scanf("%d", &n);
	while(scanf("%s", a)!=EOF)
	{
		int len = strlen(a), l, r, center;
		if(len%2 == 0)
			{l = len/2-1; r = len/2;}
		else
			{l = r = len/2;}
		center = l;
		while(l>=0 && r<len)
		{
			if(a[l] != a[r])
				break;
			l--; r++;
		}
		if(l<0 || a[l]<a[r])
		{
			l = center;
			int carry = 1;
			do{
				a[l] += carry;
				if(a[l] == '9'+1)
					a[l] = '0';
				else
					{carry = 0;break;}
				l--;
			}while(carry==1 & l>=0);
			if(l<0)
			{
				int i;
				for(i=0;i<len-1;i++)
					a[i] = '0';
				a[len-1] = '1';
				printf("1%s\n", a);
				continue;
			}
		}
		if(len%2 == 0)
			{l = len/2-1; r = len/2;}
		else
			{l = r = len/2;}
		while(l>=0 && r<len)
		{
			a[r++] = a[l--];
		}
		printf("%s\n", a);

	}
}

- Anonymous September 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//I know it is not concise, but it works.. the algorithm is simple

#include <iostream>
using namespace std;

int main()
{
int val = 57811;
int count =0;
int a[10];
int b[10];
int mode = val;
int div = 0;
//int temp = val;
int mid = 0;


while(mode)
{
mode = mode/10;
count++;
}

mode = val;
for(int i = 0; i<count; i++)
{
div = mode%10;
mode = mode/10;
a[count-1-i]=div;
//cout<<" " <<a[i];
}

if((count%2)!= 0)
mid = count/2 +1;
else
mid = count/2 ;

for(int j=0;j<mid;j++)
{
if(a[j] == a[count-1-j])
{
b[count-1-j] = a[j];
b[j] = a[j];
}
else if(a[j]>a[count-1-j])
{
b[count-1-j] = a[j];
b[j] = a[j];
}
else
{
if(j == mid -1)
b[j] = b[count-1-j] = a[j]+1;
else{
a[j+1] = a[j+1]+1;
b[j+1] = a[j+1];
b[count-j-2]=a[j+1];
a[count-j-2] = a[j+1];
b[j] = a[j];
b[count-1-j] = a[j];
}
}
}


for(int k=0;k<count;k++)
cout<<" " <<b[k];



return 0;
}

- Anonymous May 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if number is 121,its next smallest would be 131
if number is 12321, its next smallest would be 12421
So if you observe you just need to increment the middle element by 1. Simple!

- Sanjay September 29, 2015 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More