Oracle Interview Question for Software Engineer / Developers


Country: India




Comment hidden because of low score. Click to expand.
8
of 10 vote

Correct ans is 6step

Assume weights
x x x y y y a b
possible pairs
xx xy yy ab -> xx yy reject as equal, now compare result of ab & xy ->steps=5
xa xb xy yy -> reject pair yy, compare result of pair xa & xb then compare it with reult of xy ->step=6
xy xy xy ab 6-> reult of xy&xy wil be same so reject pair ->steps=6
xa yb yy xx -> same as first-> steps=5
xa yb xy xy -> steps=6
ans can be obtain in 6steps max in any case, by eliminating pair with same weight because if threre is same wait then it must be pair from 3 unit, so if we reject it then also one unit is remain of tht weight

- Ajit Deshmukh August 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

yeah.. this is optimal I believe.

- Neeraj Crespo September 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

xy xy xy ab...... how can u neglect the result of xy xy as they all are identical...... we cant differentiate b\w them

- Anshul September 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 votes

xy xy xy ab 4steps
so suppose result is x<y
x x x a
xx xa 2steps
xx equal neglect it check only xa pairs result is ans
total 6steps

- Ajit Deshmukh September 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

1. compare xxx to yyy
suppose yyy is heavier than xxx
so x <y
2. compare a to b
suppose a<b so
3. compare a to x
if a is light then answer is a
if x is light then answer is x
vice-versa for y

- baap April 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

in last possibility if we get ' x' as a result from xy
and 'a' from xa,'b' from yb
so nw fr comparison we have x x a b and i make pair of xa and xb
how it can be done 6 steps then?

- rahul August 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@rahul you're right, OP's approach cannot resolve xy xy xy ab. I would propose the following:
If all 4 groups' results are not equal, then we have
1) xy xy xy ab or 2) xa yb xy xy

Now, put the lighter balls from the first and the second group to the LHS of the scale, and put the lighter balls from the first and the second group to the RHS of the scale (that is, we have 2 balls for each side), then we have 3 possibilities:
1) min(x,y) + min(x,y) vs. min(x,y) + min(a,b)
2) min(x,a) + min(y,b) vs. min(x,y) + min(x,y)
3) min(x,a) + min(x,y) vs. min(y,b) + min(x,y)

It is not hard to show that the lightest ball must be in the lighter side, which has two balls. Compare those two balls and choose the lighter one. If they're equal, then choosing either one is fine.

Consider case 1) for example, i.e., min(x,y) + min(x,y) vs. min(x,y) + min(a,b), if the LHS is lighter, then that implies min(x,y) < min(a,b). Now let's compare the two balls on the LHS, i.e., min(x,y), min(x,y), and they turn out to be equal. Now we can pick either one and return.

- YL August 28, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@rahul I agree with you the xa yb xy xy case cannot be resolved in 6 steps following the OP's approach.

Let me propose something else. If all four groups turn out to be not equal, then we have:
(1) xy xy xy ab or (2) xa yb xy xy

Next, let us throw away the heavier ball from each group, then we have 4 balls remaining and they could be:
(1) min(x,y) min(x,y) min(x,y) min(a,b)
or
(2) min(x,a) min(y,b) min(x,y) min(x,y)

Now split the 4 balls into two groups (with 2 balls per group) and compare. There are three possibilities:
(i) min(x,y) + min(x,y) vs. min(x,y) + min(a,b)
(ii) min(x,a) + min(y,b) vs. min(x,y) + min(x,y)
(iii) min(x,a) + min(x,y) vs. min(y,b) + min(x,y)

It is not hard to show that the lightest ball (our target) must be on the lighter side of the scale. We can resolve the two candidates by using one comparison.

For example, if we had (i) min(x,y) + min(x,y) vs. min(x,y) + min(a,b) in the previous step, and the LHS is lighter. That implies min(x,y) < min(a,b). Now we compare the two balls min(x,y) and min(x,y) from the LHS, and they turn out to be equal, so we return either one as our result.

- YL August 28, 2019 | Flag
Comment hidden because of low score. Click to expand.
4
of 10 vote

i think it can be done is 5 steps:
1.make 4 group of 2 each.
2. compare 1st group with the 2nd. you come 2 now about the lighter group.
3. compare any one coin from first group with one from second. you get lighest of the 4.
4. repeat for 3rd and 4th group.
5. compare the two lighter ones to get the lightest.

- Ashish August 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

your solution seems correct if the weight balance is provided if suppose wighing machine is provided than this wont work. Kindly comment

- viji.nitb August 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

in this kind of questions, u are supposed to solve them using weighing balance only. coz the results are based on the relative weight of one compared to other. if chcking for absolute weights, u ll be needing as many no. of comparisons as the no of coins.

- Ashish August 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Ashish
you wanna say that there is two type of weights!
7 have same weights and other one does not have!

But what is a,b,x,y have different weightings! correct me if i misunderstood :)

- Psycho August 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

@Ashish in step 3. u r picking any 1 coin from each group.suppose u picked the heaviest coin from each group n by comparing these two u got the lightest among these two coins only.NOT in the all 4 coins.I think u should reconsider step 3.

- lampard August 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

x x x y y y a b
possible pairs
xx xy yy ab 5
xa xb xy yy 6
xy xy xy ab 6
xa yb yy xx 5
xa yb xy xy 6
ans can be obtain in 6steps max in any case, by eliminating pair with same weight because if threre is same wait then it must be pair from 3 unit, so if we reject it then also one uni is remain

- Anonymous August 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

7 weighing, its like finding minimum element in an array

- cobra July 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this problem will be solved by divide and conquer we dont need 7 comparisons. Dive 8 /2 = 4 sets compute the lesser on each subsets. now we will get 4/2 = 2 sets again lesser of 2 will be the smallest one.
e.g. set{3,3,3,4,4,4,2,1}
divide in to four 33, 34, 44,21 lesser of these 4 sets
3,3,4,1 create 2 sets 33, 41 lesser of these
3,1 make a set compare we will get 1 as the smallest we need only 3 comparisons. Let me know if we have a better approach.

Thanks
Bala

- Bala July 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

you also done 7 comparisons.. .(3,3), (3,4), (4,4) (2,1), (3,3),(4,1),(3,1)

- cobra July 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

if a case like (2,2)and (5,1) arises then???

- kush.udit July 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@cobra I'll suggest u to read question properly b4 answering it.

- lampard August 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

6 coins

- Akshay Kumar July 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

care to tell your algo?

- Geek August 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

wow!!well explained..

- Abhishek August 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

For 7 weighings no need to divide into groups..just compare any two first then lighter of two with another and so on... For seven weighings we can get
I don't think there is more efficient way

- Srinu August 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Math wise...5 Unknowns...5 Equations..so minimum of 5 Weighings.
3x+3y+a+b = K
x,y,a,b,K

Correct me if I am not seeing the point, there can always be a smart way!

- Dutch August 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Super wrong...All coins are identical..oops!!

- Dutch August 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

7 steps.....
do it as Merge Sort.....
make 4 groups of 1-1

- Anshul September 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think this is the worst solution. Maybe someone can build up on this(?)
1)Take any random coin and weigh each of the other coins against this one. Arrange all coins with respect to this coin on a table (i.e heavier ones to the right, and lighter ones to the left of the coin. If weights are equal, then stack them over the base coin). Now since you know which coin you used to compare with the others, you can remove all those coins and any coin heavier than that coin from the picture (remove from your table and throw them away).

Now repeat the same process taking the coin which is relatively just lighter than your previous common coin used for comparison. Eventually, you should get just 2 coins of which one will be the "lightest" of them all.

Am I wrong? This may be a bad method, but makes no assumptions whatsoever. Also, it is guaranteed to find you the absolute lightest coin in the mixture of coins.

Thanks and any feedback is appreciated!

- NK October 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a small change to Ashish 5 steps,
In the third step, compare the two elements with each other in the ligther group and the element which is ligther will be the ligther element among 4 elements(of two groups)
eg say gropA -elements wt 10,8 and Gropu B wtv12,6.Now Group A> Group B..so Group B is ligther and if compare 12 and 6..obviously 6 is least elemet among four elemrnts(10,8,12,6). Plz let me if i my understanding is wrong

- sastry.soft October 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

the combinations are:
com 1:xy xy xy ab all 4 groups distinct none off them have element with equal wt.

com 2:
ax bx xy yy
ay by xy xx these 2 combination, 1 group will have equal element

com3:
ax by xx yy
ay bx xx yy
ab xy xx yy these 3 combinations have 2 groups with equal elements


in com1 after 4 weighings btw group members you will have either x x x a or yyyb or xxxb or yyya. now again group into groups of 2 and weigh. you will find that 1 group has elemnts of same weight and other have elements of diff wts.lighter in group with diff weight is the lightest.(total 6 weighings)

in com 2: after initial 4 weighings to determine the combination.neglect the group with equal members from competition as those members are already in other groups.no weigh 1st group against 2 group and 3 group.that is 6 weighings now you know the lightest group and the lightest member of them(initial comparison).

in com3 : neglect the 2 groups with equal members as these weights will be in the other groups as well.weigh the lighter members of the other two groups and lightest of them is the lightest of all coins.so you have answer in 5 weighs.

in any combination max number of weighings is 6 except com3 then it is 5

- xcoder January 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can do this in 3 step

1. compare xxx to yyy
suppose yyy is heavier than xxx
so x <y
2. compare a to b
suppose a<b so
3. compare a to x
if a is light then answer is a
if x is light then answer is x
vice-versa for y

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

3 steps would be minimum

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

This can be easily done in 5 steps. Pick one coin and compare it with another coin. Take the lighter one and compare it with the next one. Continue this till you have compared the last coin. The lighter one in the last comparison will be the lightest.

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

3 steps:
mix the coins
1.devide coins into group of 4 and weigh(ignore less weight group)
2.again devide 2 coins to 2 coin each in group and weigh(ignore less weight group)
3.weigh single coins find which has hightest weight.

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

Good explanations where already given further up. Still for me
it is always easier to understand a concept when I can related
it somehow to the 'real world'

Think of a sports event where always the weakest wins
and moves up to the next round of comparisons. That
would require 7 'matches' to find the winner. Since there
are two sets of three, equals/'ties' can be eliminated
since there is still a third coin 'in the game' with the
same weight that would win in case it was the lightest.

c
                    c   <--  7 -->    c 
               c <--5--> c       c <--6--> c 
            c<-1->c   c<-2->c   c<-3->c   c<-4->c

There is only a limited number of combinations
possible:

- two ties on either side of the tree (left or right)
in the first round.

-
                   -    <----------> WINNER
               -----------         c <--5--> c 
              TIE      TIE		
            x<-1->x   y<-2->y   a<-3->x   b<-4->y

- one tie on each side of the tree in the first round

WINNER
                  c   <---  5 --->    c 
   	         -  <----> c         - <-----> c 
            TIE                 TIE
          x<-1->x   a<-2->x   y<-3->y   b<-4->y

- only one tie or second one in case x (or y) is
the lightest

WINNER
                  c   <---  6 --->    c 
              c <--5--> c         c <-----> - 
                                           TIE
           x<-1->a   x<-2->b   x<-3->y   y<-4->y

- one tie in the second round

-
                 TIE   <----------> WINNER 
             c <--5--> c         c <--6--> c 
          x<-1->y   x<-2->y   x<-3->a   y<-4->b

Hope it helps.

- gonzo August 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

4 cases..
suppose there are 8 coins with weight( 1,2,3,3,3,4,4,4) divide them randomly into sets of four like(1,2)(3,3)(3,4)(4,4) now compare these sets will get two sets (2 comparisons) .
again compare the least two (1 more comparison) now compare the last set (last comparison).
(1,2)(3,4)--->>(1,2)-->>1

- mohit August 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

if set is lyk (3,3)(1.6)

- steve August 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

But if dividing randomly you may end up making groups like (1,4) (2,3) (3,4) (3,4) and then if u compare them u are struck as two of them are equal...so it will not be possible in 4 cases then !!

- Bill August 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

7 weighs.
Step 1) Divide 8 coins in four pairs. In 4 weighs you will get 4 lighter coins.
Step 2) Divide 4 coins in two pairs. In 2 weighs you will get final 2 coins.
Step 3) Weigh last two coins you will get the lightest coin.
4 + 2 + 1 = 7 Weighs

- Manu August 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

The problem does not say that we need to compare in sets of two. So here is the algo

1) Divide 8 coins in 2 sets. In 1 weigh get 4 lighter coins.
2) Divide 4 coins in two sets. In 1 weighs you will get 2 lighter coins.
3) In last 1 weigh you will get the lightest coin.

Total 3 Weighs

- Kashif August 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

its not like, all weigh the same except the one.
how can you assure that the light set you get on one first weighing will have the lightest coin.

- ashish August 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

there can be a condition on weight
3y+a < 3x+b

assuming this u might suggest the lightest meight be either y or a
but cant it be b(assuming that object x are very heavy)
hence their cumulative weight would be greator than that of 3y+a

- kumsaha August 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

3 steps:
compare x units and y units coin eliminate the one with higher weight
then compare the remaining coin(either x or y) with a units coin and eliminate the one with higher weight
finally compare the remaining coin(either x or y or a) with b units coin and eliminate the one with higher weight..........!!!!!!!!!!!

- prime September 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I was wondering, it could be done in 3 steps.
Groups {XXX,YYY,A,B}
Step 1: Weight between XXX and YYY. Find the least weight in it. Namely, X
Step 2: Weight between A and B. Find the least weight among them, Namely, A
Step 3: The least weight would be among X and A. We can try one more time, and it seems we are done. :)

- codetheuniverse February 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

According to the question all the coins are mixed and they look identical... how could you divide them into different sets as xxx,yyy,A and B??

- Subrahmanyam April 17, 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