Yahoo Interview Question Software Engineer / Developers

  • yahoo-interview-questions
    0
    of 0 votes
    8
    Answers

    Write a function to add an array of numbers.

    - June on December 03, 2009 Report Duplicate | Flag
    Yahoo Software Engineer / Developer Application / UI Design Arrays C C++ Coding



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

what kind of numbers, real/integer

int add_Num(int arr[], int n ){
  
  int sum = 0;
  int j = 0;
  while (j < n ){
    sum+=arr[j];
    j++;
  }
 return sum;
}

- Anonymous on December 05, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

The obvious solution require O(n) operations. While the following one require O(lgn) operations. It may be the reason they asked the question.

int Sum(int A[], int h, int l)
{
  if (h==l) return A[l];
  else
  {
     int m=l+(h-l)/2;
      return Sum(A,l,m-1)+Sum(A,m+1,h);		
  }
	
}

- Anonymous on February 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

yahoo asks such trivial questions ??

- Anonymous on December 25, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Write a template function?

- creationw on December 28, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

template <class T>
T sum(T* arr, int num){
T sum=0;
for(int i=0;i<num;i++){
sum += arr[i];
}
return sum;
}

- papaya on January 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think the question here is that two numbers (multi digit) are represented as arrays - Each element of an array containing a digit and the numbers have to be added.

- Anonymous on May 25, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

/* program to add two numbers. each number is represented by an int array. 
 * each element in the array is an number */ 

#include <iostream>
using namespace std; 

void PrintNumber(int* array, int len)
{
	for( int i = 0; i<len; i++ )
		cout << array[i]; 
	cout << endl;
}

void reverse_array(int* array, int len)
{
	int* p = array; 
	int* q = p + (len-1); 
	int temp; 
	while( p < q )
	{
		temp = *p; 
		*p = *q; 
		*q = temp; 
		p++; 
		q--; 
	}
}

void AddNumbers(int* array1, int len1, int* array2, int len2, int** array3, int* len3)
{
	int max_len = max(len1, len2); 
	int* sum_array = (int*)malloc(sizeof(int) * (max_len+1)); //+1 for carry
   	
	int i, j;  
	int k = 0;
	int carry = 0; 
	for( i = len1-1, j = len2-1; i >= 0 && j >=0; i--, j--, k++ )
	{
		if( (array1[i] + array2[j] + carry) > 9 )
		{
			sum_array[k] = (array1[i] + array2[j] + carry) % 10; 
			carry = 1; 
		}
		else
		{
			sum_array[k] = array1[i] + array2[j] + carry; 
			carry = 0; 
		}
	}	

	if( i > 0 )
	{
		for( ; i >= 0; i--, k++ )
		{
			if( (array1[i] + carry) > 9 )
			{
				sum_array[k] = (array1[i] + carry)%10; 
				carry = 1; 
			}
			else
			{
				sum_array[k] = array1[i] + carry; 
				carry = 0; 
			}
		}
	}
	else if( j > 0 )
	{
		for( ; j >= 0; j--, k++ )
		{
			if( (array2[j] + carry) > 9 )
			{
				sum_array[k] = (array2[j] + carry)%10; 
				carry = 1; 
			}
			else
			{
				sum_array[k] = array2[j] + carry; 
				carry = 0; 
			}
		}
	}

	sum_array[k] = carry; 

	reverse_array(sum_array, max_len+1); 
	*array3 = sum_array; 
	*len3 = max_len+1;
}


int main()
{
	int array1[] = { 1, 1, 1, 1 }; 
	int array2[] = { 9, 9 }; 

	PrintNumber(array1,sizeof(array1)/sizeof(array1[0])); 
	PrintNumber(array2,sizeof(array2)/sizeof(array2[0])); 

	int* array3 = NULL; 
	int len = 0; 
	AddNumbers(array1, sizeof(array1)/sizeof(array1[0]), array2, sizeof(array2)/sizeof(array2[0]), &array3, &len); 
	cout << "done with adding numbers" << endl;
	PrintNumber(array3,len); 
	
	return 0; 
}

- Girish on October 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

May be they are looking for this

(defun sum-list (list)
  (if list
      (+ (car list) (sum-list (cdr list)))
    0))

- kumar.akshay on June 01, 2012 | Flag Reply


Add a Comment
Name:

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

Books

is a comprehensive book walking you through every aspect of getting a job at a top tech company, while focuses on software engineering interviews.

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