NVIDIA Agilent Technologies Interview Question for Software Engineer / Developers






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

/* Return value:
integer value of the string passed

Functionality:
Below function works for examples as shown below:
"0" -> output = 0
"1" -> output = 1
"123" -> output = 123
"-1" -> output = -1
"+1" -> output = 1
" 0" -> output = 0
" -1" -> output = -1
" +1" -> output = 1
"00" -> output = 0
"-01" -> output = -1
"+01" -> output = 1
"00123" -> output = 123
"12300" -> output = 12300
" -0123" -> output = -123
" +0123" -> output = 123
"*123" -> output = 123
"123*" -> output = 123
"*123*" -> output = 123
"*-123" -> output = -123
“*+123” -> output = 123
“123-“ -> output = 123
“123+” -> output = 123
"*12*3" -> illegal input -> undefined output = 12
"*-*12" -> illegal input -> undefined output = 0

Algorithm:
1. Skip any non digit characters, which are not '-'
2. If you hit a '-', it means its a negative number. Skip '-' and make
is_negative boolean true.
3. From this point, while you have continuous digits, do
a. Every iteration of the digit, multiply the int_val by 10 and add (digit value%10)
4. If is_negative is true, multiply the int_val by -1 and return,
else return int_val */
int atoi(char *str)
{
int is_negative = 0;
int int_val = 0;

/* Skip non digit characters excluding '-' */
while (!isdigit(*str) && (*str != '-'))
{
str++;
}
/* Number is negative */
if (*str == '-')
{
is_negative = 1;
str++;
}

/* Compute the int_val for contiguous digits */
while (isdigit(*str))
{
int_val = (int_val*10) + (*str - '0')%10;
str++;
}
return ((is_negative) ? (int_val * -1): int_val);
}

- PR March 31, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It will die if input string is "asfbsdf" so little modification...
while (!isdigit(*str) && (*str != '-'))
{
str++;
}
should be ...
while (!isdigit(*str) && (*str != '-'))
{
if(*str=='\0')
{
printf("undefined output");
return o;
}
str++;
}

- Dhaval May 19, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Similar checks for '\0' should be present in other parts as well!

- Dhaval May 19, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Kewl solution. I like that int_val = (int_val*10) + (*str - '0')%10; It is very cryptic and really nice.

Here is a similar solution...just a different framework.

#include <iostream.h>
long Atoi(const char*);
int main(void)
{
while(1)
{
char s[20];
cout<<"Enter string to convert to integer (q to quit): ";
cin>>s;
if(!strcmp(s,"q")) break;
cout<<"Standard atoi function's output: "<<atoi(s)<<endl;
cout<<"Custom made Atoi function's output: "<<Atoi(s)<<endl;
}
}
long Atoi(const char *str)
{
short negative = 1;
long intVal=0;
if(str[0] == '-')
{
negative = -1;
str++;
}
else if(str[0] == '+')
str++;
while(*str >= '0' && *str++ <= '9')
intVal = intVal*10 + (*(str-1)-'0');
return negative*intVal;
}

- cc June 06, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

"It is very cryptic and really nice."

ha ha ha...

- SomeSDE June 10, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool bIsNav=false;
int iRet;

if(*chr='-')
{
bIsNav=true;
chr++;
}
else if(*chr=='+')
{
chr++;
}

while (chr!=null)
{
if(*chr>='0'&&*chr!='9')
{
iRet=iRet*10+(*chr);
}
else
{
system.out("this is not a integer");
}

if (bIsNav)
return -iRet
else
return iRet;
}

- Have fun July 02, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/* with minimal error checking

unsigned int atoi(char* number) {

if (number == NULL || strcmp(number,"") == 0) {
return 0;
}

unsigned int iNumber = 0;

while (number != NULL && strcmp(number,"") != 0) {
if(*number <= '9' && *number >= '0' ) {
iNumber = (iNumber << 3) + (iNumber << 1)+(*number++ - '0');
}
else {
return 0;
}
}

return iNumber;
}

- p-r-a-b August 16, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <string.h>
#include <math.h>

unsigned int atoi(char* number);

int main() {
int number;

number = atoi("35.223");
printf("Number is %d\n",number);


return 0;
}


int atoi(char* number) {
int iNumber = 0; //initialize it to 0
int loopIndex;//index that loops through the number string
int strLen = 0;
char *decimalPoint;

if(number != NULL) { //string points to a null character
if(strcmp(number, "") != 0) {//check if the string is not empty
strLen = strlen(number);
if(strchr(number, '.') != NULL) {
decimalPoint = strrchr(number, '.'); //Truncate the digits after the decimal point
strLen = decimalPoint-number;
}

if(number != NULL) {
for(loopIndex = strLen-1; loopIndex >= 0; loopIndex--) {
if(number[loopIndex] <= '9' || number[loopIndex] >= '0') {
iNumber+=(pow(10, ((strLen-1)-loopIndex)))*(number[loopIndex]-'0');
}
}
}
}
}
return iNumber;
}

- Manish August 17, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

uint atoi(char* str)
{
if (!s ) return 0;
int sum = 0;
int mult = 0;
int sign = 1;

if (*s == '-')
{
++s;
sign = -1;
}

while (*s)
{
if (*s <= '0' && *s >= 9) break;
sum = sum*mult + (*s - '0');
++s;
mult *= 10;
}

sum *= sign;

return sum;
}

- vivek August 19, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

return int and not uint as we are taking care of '-' sign in above program. the earlier post is taking care of decimal. that would be dtoi and not atoi. and we should be using strcmp and strlen etc which are single pass over string.

- vivek August 19, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think there is one problem in vivek code:

sum = sum*mult + (*s - '0');

should be:

sum = sum*10 + (*s - '0');

and remove all "mult" from the code.

- New October 04, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int sum=0;
int mult=1;

int start= first(s,'.'); // find first occurance of .
if (start==-1) start=strlen(s)-1; // no ., start at end

for ( i=start; i>=0 ; i--) {
int v= (s[i]-'0') * mult;
sum = sum + v;
}

// yeah, I assume ascii - it wont work with ebcdic or other character sets with holes
// yea, I assume no commas.
// yeah, I didn't write first()... most langs have such a fuction to find the first occurance of a character in a string

- Thomas S November 03, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int sum=0;
int mult=1;

int start= first(s,'.'); // find first occurance of .
start--;
if (start < 0 ) start=strlen(s)-1; // no ., start at end

for ( i=start; i>=0 ; i--) {
int v= (s[i]-'0') * mult;
sum = sum + v;
}

// yeah, I assume ascii - it wont work with ebcdic or other character sets with holes
// yea, I assume no commas.
// yeah, I didn't write first()... most langs have such a fuction to find the first occurance of a character in a string

- Thomas S November 03, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<limits.h>

typedef enum {
WHITE_SPACE,
GETDIGITS,
FINISHED
} get_state;

typedef enum {
POSITIVE,
NEGATIVE
} get_sign;


int get_int(char * ch){

get_state state = WHITE_SPACE;
get_sign sign = POSITIVE;


int value = 0;
int limit = INT_MAX / 10;


int dig;


if(*ch){
while(state != FINISHED){
dig = (unsigned int)*ch++;

switch(state){

case WHITE_SPACE:
if(dig == ' '){
;
}

else if(dig == '+'){
state = GETDIGITS;
}

else if(dig == '-'){
state = GETDIGITS;
sign = NEGATIVE;
}
else if(dig >= '0' && dig <= '9'){
value = dig - '0';
state = GETDIGITS;
}
else{
state=FINISHED;
}

break;

case GETDIGITS:
dig = dig - '0';
if( !( ( dig >= 0 ) && ( dig <= 9 ) ) ){
state=FINISHED;
break;
}

if( value < limit ||
( (value ==limit) &&
(((sign == POSITIVE) && (INT_MAX - value * 10 >= dig))
|| ((sign == NEGATIVE) && (- INT_MIN - value * 10 >= dig)))) ){
value = value * 10 + dig ;
}
else{
if(sign == NEGATIVE)
value=INT_MIN;
else
value=INT_MAX;

state=FINISHED;
}
break;

case FINISHED:

default:
break;

}
}

}
if( sign == NEGATIVE )
value = value * -1;

return value;

}

int main(){

char str[20];

while(1){

printf("\nEnter Number: ");

fgets(str , sizeof(str),stdin);

fflush(stdin);

printf("\nYou Entered = %d ",get_int(str));
}

}

- Ravi Kant Pandey May 21, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

// convert.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

int power( int x, int n)
{
if(n == 0)
return 1;
else
return x * power( x , n - 1 );
}
int my_atoi(const char *input)
{
int return_value = 0;
int digit_count = 0;
if ( ! input ) return 0;
char *temp = (char*)input;
/* Check if the input contain any alphabets */
while ( *temp != '\0' )
{
if (((int)(*temp) < 48 ) || ((int)(*temp) > 57 ) )
{
return return_value;
}
digit_count++;
temp++;
}
temp = (char*)input;
digit_count--;
while ( *temp != '\0' )
{
int t = power( 10, digit_count-- );
int m = (char)(*temp) - '0';

return_value = return_value + (t * m);
temp++;
}
return return_value;
}
int _tmain(int argc, _TCHAR* argv[])
{
printf ("-----> %d\n",my_atoi(""));
printf ("-----> %d\n",my_atoi("12"));
printf ("-----> %d\n",my_atoi("12345"));
printf ("-----> %d\n",my_atoi("5555"));
printf ("-----> %d\n",my_atoi("12A45"));

return 0;
}

- AD January 22, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is my code...

#include<stdio.h>
void main()
{
	char str[100];
	char * ptr;
	printf("Enter the string \n");
	ptr = gets(str);
	int i =0;
	int val = 0;
	while(str[i]!='\0' && (str[i] >='0' && str[i] <='9' || str[i] == '-'))
	{
		if(str[i] == '-')
		{
			i++;
			continue;
		}
		val = str[i]-'0' + val*10;
		i++;
	}
	if(str[0] == '-')
		printf("The value is %d \n",0- val);
	else
		printf("The value is %d \n", val);
}

- gauravk.18 February 29, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

here's my method:

1) kick out all characters which is not '+','-','0'-'9', make a new string;
2) in the new string, if '+','-' does not occur at first place, illegal;
3) other wise, generate the number of "pure-digits" number and add with the corresponding sign.

- Anonymous April 04, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int myatoi(const char* ptr)
{
int n = 0;
for(; *ptr <= '9' && *ptr >= '0'; ptr++)
{
n = n * 10 + (*ptr - '0');
}

return n;
}

- O April 07, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

example atoi('10', 2) returns 2 in decimal

- how about implementing int atoi (const char * str, size_t radix) where radix is the base??? May 13, 2008 | 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