Facebook Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
0
of 2 vote

int a2i(char* str)
{
   int num = 0;
   int i=0;
   
   for(i=0;i<strlen(str);i++)
   {
      num = num*10 + str[i] - '0';
   }
 
 return num;
}

- December 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This fails for many test cases... ex: -1, +1

- S December 14, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

make it num += num*10 + str[i] - '0';. for positive and negative you can use flag and make the return value positive and negative depending upon that

- richa.shrma.iitd April 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

@Richa: num += num*10 + str[i] - '0'
is incorrect.

- susmita.agnihotri May 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="c++" line="1" title="CodeMonkey52824" class="run-this">int num = 0;
int mul = 1;

for(int i=0; i < strlen(str); mul *= 10, ++i)
{
num += (str[i] - 0x30) * mul;
}

</pre><pre title="CodeMonkey52824" input="yes">
</pre>

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

in the for loop
{
num*=10;
num+= char[i] - '0';
}

- Srikanth Basappa December 14, 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 b[]="-40";
int flag=1;
int num=0;
for(unsigned int i=0;i<strlen(b);i++){
if(b[i]=='-') flag=-1;
else num=num*10+b[i]-'0';
}
printf("%d",flag*num);
}

- Neo December 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int Ascii_To_Int (char * str)
{
int num = 0;
int mul = 1;
int End = strlen(str);

for(int i = End-1; i >= 0 ; mul *= 10, i--)
{
num += (str[i] - 0x30) * mul;
}

return num;
}

- Travolque January 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I didn't get the question correctly. can any one post a sample input and output value?

- jimmymani January 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what do you mean by ASCII representation? Is it any different from string representation

- div January 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what about int overflow?

- pz February 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int atoi(char *str, int strLen){
int sign = 1;
int i = 0;
int result = 0;

if(str == NULL) return -1;
if(str[i] == '-') {
sign = -1;
i++;
}

while (i < strLen){
if(isdigit(str[i])){
checkIntOverflow(result, INT_MAX);
result = result*10 + str[i] - '0';
}else{
fprintf(stderr, "not valid integer string\n");
}
i++;
}
return result;
}

- AW April 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

enum err_t
{
	ERR_OK,
	ERR_ARG,
	ERR_INVAL,
	ERR_OVERFLOW,
};
typedef int BOOL;
#define TRUE 1
#define FALSE 0

err_t atoi(const char *str, int *num)
{
	BOOL negative = FALSE;
	err_t err = ERR_OK;
	unsigned int result = 0, temp;
	unsigned int addition;

	if (num == NULL) {
		err = ERR_ARG;
		goto out;
	}

	if (str == NULL) {
		err = ERR_ARG;
		goto out;
	}

	if (*str == '-') {
		negative = TRUE;
		++str;
	} else if (*str == '+') {
		++str;
	}

	while (*str) {
		if (*str >= '0' && *str <= '9') {
			addition = (unsigned int)(*str - '0');
			if (0 != (result & (0xF0 << (8 * (sizeof(unsigned int) - 1))))) {
				err = ERR_OVERFLOW;
				goto out;
			} 

			temp = result << 1; // temp = result * 2;
			result <<= 3;  // result *= 8;

			if (temp + result < result) {
				err = ERR_OVERFLOW;
				goto out;
			}
			result = temp + result;  // finally multiplied by 10, NO overflow.

			if (addition + result < result) {
				err = ERR_OVERFLOW;
				goto out;
			}
			result += addition;
			++str;
		} else {
			err = ERR_INVAL;
		}
	}

	if (0 != (result & (1 << (8 * sizeof(unsigned int) - 1)))) {
		err = ERR_OVERFLOW;
		goto out;
	}

	*num = (int)result;
	if (negative) {
		*num *= -1;
	}

out:
	return err;
}

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

int Atoi(const char * ptr) {
  long long rs = 0;
  bool minus = false;
  while (*ptr == ' ') ptr++;
  if (*ptr == '-') {
    minus = true;
    ptr++;
  } else if (*ptr == '+') ptr++;
  while (*ptr >= '0' && *ptr <= '9') {
    rs = rs * 10 + *ptr - '0';
    ptr++;
    if (rs > INT_MAX) break;
  }
  if (minus) rs *= -1;
  if (rs > INT_MAX) return INT_MAX;
  if (rs < INT_MIN) return INT_MIN;
  return rs; 
}

- guoliqiang2006 January 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

user atoi

- pankaj January 28, 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