Interview Question for Java Developers


Country: UK
Interview Type: Written Test




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

In C logic will be like this:

int func(int a,int b)
{
int result=0,i,j;

for(int i=0;i<b;i++)
{
for(int j=0;j<a;j++)
{
result++;
}
}

return result;

}

- Vipan k Verma May 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

there are many pitfalls in that case.What about 100! * 100! ?
@eugene.yarovoi: I think your case can be easily handled by taking absolute values and checking the signs?If two negatives then result will be positive and if one negative then result will be negative.

- aka May 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

eugene.yarovoi
you provide nice test case to check solution.
thanks u

- Vipan k Verma May 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If that's C code, it won't compile - i,j are double declared.

- EugenDu May 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

int Mul(int a, int b)
 {
  int res = 0;
  for(int i = 0 ; i < b; i++)
	for(int j =0 ; j< a ; j++)
		res++;
 return res;
}

- Putta May 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You may also use recursive function:

Multiply(a, b)
{
	if(b == 0)
	{
		return a;
	}
	else
	{
		return Multiply(a++, b--);
	}
}

However, I think b-- is an arithmetic operation...

- sid78669 May 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

By using post increment and post decrement operator a and b will never change its value as in post operation first of all it assign value than increment.
so your code will be in infinite loop.

example
b=5
a = b++;

a =5 and b =6 will return;


And if go with logic it will just add b's value to a.

example
a =5 , b =6
return 11 only
required is 30.

- Vipan K Verma May 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

this is almost correct, but even if you fix the post-incrementation bug, the logic still isnt there.

you are stating in your code that addition is recursive and decrements the value to be added along with the times it is added. this is false.

actually, it should be stated as recursive addition decrementing only the times added.

the inductive base case is when the first value is 1, which returns the first value, which is how multiplication is defined (i.e. 1*x = x)

example code that works in java

static int mult(int a, int b)
	{
		if(b == 0 || a == 0)
		{
			return 0;
		}
		else if(b == 1)
		{
			return a;
		}
		else
		{
			return a + mult(a, b - 1);
		}
	}

You can also claim this code works because it is adding -1 not taking away 1.

you also should take into account when the values are 0 -> 0

- Anonymous May 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int multiply(int n, int m) {

		int temp = n;
		for(int i = 0; i < (m-1); i++) {
			for(int j = 0; j < n; j++) {
				temp++;
			}
		}
		
		return temp;
	}

- ram sharma May 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

same solution mentioned in first comment.

- Vipan k Verma May 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Int K= 1;
Int i=1; j=1;
For( i= 1;i<=m;i++ )
For( J=1 ; j<=n; J++)
k++;

System.out.println(" M*N = " + k )

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

//hope this would satisfy for all multiplication operands
#include<stdio.h>
int main()
{
int n,i,j,m,result=0;
printf("enter the two numbers for product:\n");
scanf("%d%d",&n,&m);
for(i=1;i<=abs(m);i++)
for(j=1;j<=abs(n);j++)result++;
if(((m<0)&&!(n<0))||(!(m<0)&&(n<0))){
result=(~result);result++;}
printf("thus the product of two numbers is :%d",result);
return 0;
}

- ram rs May 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

here no arithmetic opertion, I think all are improper.
the road using ++/+ is arithmetic, how about logic bitwise operation
Int K= 1;
unsigned int p=1;
Int i=1; j=1;
For( i= 1;i<=m;i++ )
For( J=1 ; j<=n; J++)
p<<1;
k+=p;
System.out.println(" M*N = " + k )

does that sound the right answer

- bob December 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

change K+=p into
k=k||p;

so totally there is no arithmetic operation

- bob December 22, 2013 | 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