Interview Question for Java Developers


Country: UK
Interview Type: Written Test




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

I am confused...
the assumption is
if (a-b)=0, then a=b, but we have not defined equal, that means we cannot use "if(a-b)==0)

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

in c,c++; Equality can be defined as:
isequal(int a,int b){
return!(a-b);
}

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

LOL@prithvi.

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

since nonEquality is also not defined, start with
Nonequality(a,b){
return minus(a,b);
}

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

//GREATER THAN FUNCTION
bool greaterThan(int N1, int N2){
if (MINUS(N1, N2)> 0) return true;
else return false;
}

//EQUALITY FUNCTION
bool equality(int N1, int N2){
if (MINUS(N1, N2)==0) return true;
else return false;
}

//INEQUALITY FUNCTION
bool inequality(int N1, int N2){
return !(equality(N1, N2));
}

//DIVIDE FUNCTION
int divide(int N1, int N2){
int res = 0, temp = N1;
while(temp>= N2){
N2 -= N1;
res++;
}
return res;
}

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

Although it looks correct (and maybe I am not seeing something), but you might endup having infinite recursion there. In greaterthan, you are reusing greaterthan to compare with 0, and in equatilty you are reusing equality. hence, in either cases (in my silly opinion) you will not hit the case to return false.

However, if I missed something, please point out, I am really interested in the solution to this.

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

lol I forgot to comment those lines, fixed it now.

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

Without restrictions let use minus and increment functions
Equality: a-b = 0
Inequality: a -b != 0
Greater than: a - b > 0
Divide: increment 0...b, and minus

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

You can't use > in the definition of >

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

Incomplete question.

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

Edited

Equality: 
in c, return (a-b); false is returned for equality;// i am still thinking its java solution

Inequality: a -b !Equality 0

for b>a:
if(((a-b) >> 31)=1 and (a inequality b)) a is greater// for other data types (not 32 bit representation) similar shifting.
else b is greater

a/b
c= 0, temp= a;
while(temp>= b){
b-= a;
c++;
}
c is the answer

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

a = 10
b = 8
result = 0

loop(b) {
   last = 0
   times = 0;
   loop(a) {
      last = times
      times = inc(times)
   }
   result = a = last
}

- Putta May 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