Google Interview Question for Software Engineer / Developers






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

Increment the rightmost digit and keep passing the ten's digit leftwards as long as there is one. Prepend a '1' if it moves beyond the 1st digit.

- Jagat July 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I guess this is what they need?

bool IncIntChar(char*a,int s)
{
   bool flag=0;
   if(*(a+1)!=0) 
   {
   flag=IncIntChar(a+1,s);
   }
   if(strlen(a)<s)
   {
	  if(strlen(a)==1)
	  {
			if(*a=='9')  {
			   *a= '0';
			   flag=1;
		   }
			 else *a=*a+1;
	  }
	  else
	  {
		if(flag)
	   {
			if(*a=='9')  {
			   *a= '0';
			   flag=1;
		   }
			 else 
			 {
				 *a=*a+1;
				 flag=0;
			}
	   }
	  }
   }
   else 
   {
   if(flag==1) *a='1';
   else *a='0';
   printf("%s\n",a);
   }
   return flag;
}

- Charles December 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Was atoi and itoa not allowed? Otherwise....

char *charIncr (char *myStr) {

int myInt = atoi(myStr)+1;
myStr[0] = '\0'; //Can be ignored

int strLen = strlen(myStr);
char *newStr = (char*)malloc(sizeof(char)*(strLen+1));
itoa(myInt,newStr,10);

return newStr;
}

- kala kutta July 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The interviewer said that the string can be used to represent arbitrarily big numbers. In that case, atoi, itoa, won't work.

- Newbie August 02, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

char* Increment(char* input)
{
char *p = input;
bool isAllNines = true;
while(*p!= '\n')
{
if(*p-'0' != 9)

}
}

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

int len=strlen(s);

if((s[len-1])-'0'<9)
s[len-1]++;

else{
while(s[len-1]-'0'==9)
{
s[len-1]='0';
len--;
}
s[len-1]++;
}

- aditya August 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char *addStringByOne(const char *src) {
if(!src)
return NULL;
size_t len = strlen(src);
int i, j;
int base, tmp;
char t;
char *ret = (char*)malloc(len + 2);
if(!ret)
return NULL;
for(i = len - 1; i >= 0; i--) {
ret[len - 1 - i] = src[i];
}
tmp = 1;
for(i = 0; i < len; i++) {
base = ret[i] - '0' + tmp;
ret[i] = (base % 10) + '0';
tmp = base / 10;
}
if(tmp) {
ret[i++] = tmp + '0';
}
ret[i] = '\0';
--i;
for(j = 0; j < i; j++, i--) {
t = ret[j];
ret[j] = ret[i];
ret[i] = t;
}
return ret;
}

- anonymous August 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Another optimized method:

char *Increment(char *src) {
int n=strlen(src),i;
for(i=n-1;i;i--){
if(src[i]<'9'){
str[i]+=1;
break;
}else{
str[i]='0';
if(i==0){
str=(char *)realloc(str,n+2);
str[0]='1',str[n]='0',str[n+1]='\0';
}
}
}
return str;
}

- mail.anilkumargv October 03, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<string.h>
main()
{
int i,l,j;char s[1000];
scanf("%s",s);
l=strlen(s);
j=l-1;
if(s[j]=='9')
{
while( j>=1)
{
if(s[j]=='9')
s[j]='0';
else break;
j--;
}
if(s[0]=='9')
{
s[0]='1';s[l]='0'; s[l+1]='\0';
}
else
s[j]=s[j]-'0'+1+48;
}
else
s[l-1]=s[l-1]-'0'+1+48;
printf("%s",s);
}

- aditya rajhans October 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just increment the last character by changing it to int ...xcept the case of '9'
in case of '9' check out '9' is upto which place , if it is not upto s[0] then
make all those char to '0' and increment previous one.
if it is upto 0th place then add 1 more character ::)

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

Just increment the last character by changing it to int ...xcept the case of '9'
in case of '9' check out '9' is upto which place , if it is not upto s[0] then
make all those char to '0' and increment previous one.
if it is upto 0th place then add 1 more character ::)

- aditya rajhans October 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Allocate an array of MAX_NUM_OF_DIGITS and memset it with '0'
start from the end of array and assign it '\0'.
Have a pointer print_ptr to the at last but one position in the array. We can use this for printing the array any given time and as a pointer to most significant digit.
Rest is usual increment the counter and check if the digit reached '9' then we move left. and if the we reached the msb then we need to move the print_ptr left too.
Once the print_ptr reaches the array[0] and the value reaches '9' and a request to increment comes in we just reset the whole procedure and start from the begining

- Abhishek February 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int sum(char *s)
{
- int carry;
| if (!*s)
- return 1;
| carry = sum(s+1);
| if(carry){
- if(*s=='9'){
- *s = '0';
3 return 1;
2 }
2 else {
- *s = *s + 1;
3 return 0;
2 }
| }
| return 0;
}

int main(int argc,char* argv[])
{
char *s = argv[1];
int c = sum(s);
- if(c) {
- char *t = (char*)malloc(strlen(s)+1);
2 strcpy(t+1,s);
2 *t= '1';
| printf("%s",t);
| return 0;
| }
| printf("%s",s);
}

- nkb December 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char increment(char *number, int len)
{
	int c = 1; /* possible carry return */
	if( len <= 0 )
		return '\0'; /* huh? */

	while( c && len > 0 ) /* got something to add to a valid string */
	{
		sum = number[len-1] - '0' + c;
		if( sum > 10 )
		{
			sum	= 	sum%10;
			c	=	sum/10;
		}
		else
		{
			c	=	0;
		}
		number[len-1] = sum + '0';
		len --;
	}
	return c + '0';
}

- confused_banda November 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

char *increment(char *str)
{
	int k;
	int add = 1;
	int l = strlen(str);
	
	if (l == 0) return NULL;

	while(True && l>0)
	{
		k = char2int(str[l-1]);
		k = k + add;
		str[l-1] = int2char(k%10); //Update the Number
		add = k/10;
		if(add == 0) //If no carry over
			break;
		else
		{
			l--;
		}
	}
	if(l==0 && add !=0)
	{
		char *new_str = (char*)malloc(sizeof(char)+1);
		new_str[0] = int2char(add);
		for(int i=0;i<l; i++)
		{
			new_str[i+1] = str[i];
		}
		return new_str;
	}
	return str;
}

- SRB July 27, 2011 | 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