Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

void PrintArray(int N, int count, int sum)
{
if(count > N)
return;
else
{
Sum = Sum + pow(count, 2);
printf("%d", Sum);
PrintArray(N, count+1, Sum);
}
}

- DashDash April 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Follow-on question: what if you (for whatever reason) couldn't use the multiplication operator?

- DJ April 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1^2+2^2+N^2 = N*(N+1)*(2*N+1)/6,so,the next you know..

- zhangzhiq000 April 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Based on my understanding:

int[] populateArray (int n)
{
    if (n < 1)
       return -1;
    else
    {
         int[ ] arr = new int[n];
         a[0] = 1;
         for (int i = 1; i < n; i++)
              arr[i] = pow(i + 1, 2) + arr[i - 1];
         return arr; 
    }

}

- sma.jazayeri April 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def square_sum(n):
    return (n*(n+1)*(2*n+1))/6

def print_array(n):
    print [square_sum(e) for e in range(1,n+1)]

print_array(3)

- spicavigo April 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Above answers confused me. If I have understood correctly, here is the solution.

{
	int currentSum = 0;
	
	for (int i = 1; i <= n; i++)
	{
		currentSum += i*i;
		cout << currentSum << " ";
	}
}

- Yogesh Sharma April 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void cupQuest1(int n, int * output )
{
	if ( n < 0 ) return;

	output = new int[n];
	
	output[0] = 1;	

	for (int i = 2; i <= n; i++)
	{
		output[i-1] = i*i + output[i-2];		
	}	
}

- Yogesh Sharma April 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It is pretty much similar to generating fibonicci series . I used the same algorithm to generate the array of sum of square. Infact this can be modified to generate the array of sum/multiplication of square/cube.

package example.datastructure.interview;

import java.util.ArrayList;

/**
Given an integer N, populate an array of size N with the first N sum-of-squares.
In other words, if you were given N=3, your array would be [1, 5, 14] (1^2, 1^2 + 2^2, 1^2 + 2^2 + 3^2).
*/

public class FindSumOfSqaure {

/**
* @param args
*/
public static void main(String[] args) {

ArrayList<Integer> list = new ArrayList<Integer>();

// sum of square for n=5;

for (int i=1; i <=5; i++)
{
list.add(new Integer(sumofSquare(i)));
}

System.out.println(list);

}

static int sumofSquare(int num)
{
if (num==1)
return 1;

return sumofSquare(num-1)+ num*num;
}
}

- Prashant April 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String args[])
{
System.out.println("Enter the size of array:");
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int a[] = new int[n], temp=0,j, sum=0;

for(int i=1,k=0;i<=n;i++,k++)
{
sum = sum + i*i;
a[k] = sum;
}
for (int i=0;i<a.length;i++)
{
System.out.println(a[i]);
}
}

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

Using recursion but the fibonacci concept
int a[100];
void sum_of_2(int a_index, int target)
{
if(a_index == target)
return;
a[a_index] = a[a_index-1] + (a_index+1)*(a_index+1);
printf("%d\n", a[a_index]);
sum_of_2(a_index+1, target);
}

int main()
{
memset(a, 0, 100*sizeof(int));
a[0] = 1;
sum_of_2(0, 100);
}

- aka April 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void main()
{
int n,i,j,a[10];
clrscr();
printf("Enter your number : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
int countsum=0;
for(j=1;j<=i;j++)
{
countsum+=j*j;
}
a[i-1]=countsum;
}
for(i=0;i<n;i++)
{
printf("\n%d",a[i]);
}
}

- karthi G April 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int[] populateArray(int[] arr,int n){
int sum=0;
for(int i=1;i<=n;i++){
sum+=i*i;
arr[i-1]=sum;
}
return arr;
}

- Karthik Vvs April 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

1^2+2^2+N^2 = N*(N+1)*(2*N+1),so the next you know.

- zhixiang.zhang@hulu.com April 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@zhixiang
how you get the equation ???

(1^2 ) (1^2 +2^2) (1^2+ 2^2+3^2) ...... .(1^2+2^2+3^2+......+(n-1)^+n^2)
=>integral(n=1 to n=infinite)(n-1)^+n^2
=>2*n^2 -2n +1

- abc April 25, 2013 | Flag


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