Oracle Interview Question for Java Developers


Country: India
Interview Type: In-Person




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

I can prove that it is possible to do it in n - 1 weighings, but proving it is the minimum is escaping me.

The coins must be weighed in pairs, since there is no useful information that can be guaranteed to be gotten by weighing 3 or more coins at once.

Lemma: 2^i coins requires 2^i - 1 weighings. Proof: For i = 0, done. Suppose it holds for i. 2^(i + 1) coins can be paired mutually exclusively into 2^i pairs for weighing, resulting in 2^i coins left. Those coins require 2^i - 1 weighings by inductive hypothesis, so the # of weighings required = 2^i + 2^i - 1 = 2^(i + 1) - 1.

For n = 1, there are no weighings required - so n - 1 = 0.

Suppose that for all n with 2^i as the highest power of 2 contained in n, n coins require n - 1 weighings.

Now suppose that 2^(i + 1) is the highest power of 2 contained in n. Then we may say that m = n - 2^(i + 1) < 2^(i + 1) (if not, then n >= 2^(i + 1) + 2^(i + 1) = 2^(i + 2), a contradiction to the assumption).

By inductive hypothesis, the # of coin flips for the min weight coin out of the m remaining coins is m - 1.

By lemma, the coin flips for the min weight coin out of the 2^(i + 1) coins is 2^(i + 1) - 1.

Since we are left with two coins, we must make 1 additional weighing to determine the lightest coin, so we made (m - 1) + (2^(i + 1) - 1) + 1 = (n - 2^(i + 1) - 1) + 2^(i + 1) = n - 1 weighings.

- Wesley Cho April 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think the answer is 9 for n it is n-1.

- bajibabu March 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Its n-1.
Assume there are n players in a tennis singles tournament having the rule that as soon as someone loses a match, he is out. Now to find a winner, n-1 players must lose a match. Now see the analogy between this example and the question

- mitruka.abhishek April 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

As per your argument, that as soon as a person loses a match, he is out of the tournament, there will be approx. log n games. An example is Wimbledon, in which 128 players play in 7 rounds to find the winner.

A similar analogy would be trying to find out the tallest among a group of people, being able to ask only if person A is taller than person B, and being answered truthfully either yes / no

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

@Sandeep, your logic is wrong, I agree that for 128 players there will be 7 rounds but there will be 127 matches : 64 in first, 32 in second, 16 in third, 8 in fourth, 4 in fifth, 2 in sixth and 1 in seventh round.
You can see the question in this way as well : if a coin is rejected that it can not be the lightest one, it must have been weighted against atleast one coin lighter than itself.

- mitruka.abhishek August 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How to find min-weight (coin) in a given set of '10' distinct weights (coins) in minimum number of weighing's if provided with a balance. (no labels shown on coin resembling the weight). Generalize for 'n' distinct weights.

^-- I hate problems that are stated ambiguously.

"How to find the min-weight (coin)" => implies there's a coin in the set that weights less than the others coins in the set. Stated alternately, this implies there are two different types of coins (i.e. two possible weights for each specific coin in the set) and nine of the ten coins are of heavier variety while the remaining one is of lighter variety. But it's not clear that's what's being asked at all...

"... Given a set of '10' distinct weights (coins)" => ambiguous. could mean there are ten coins in the set each of which has a distinct weight (i.e. each coin in the set of ten has a different (i.e. distinct) weight) or there are ten coins in the set which each has a weight which could be the same or different. (would seem like an imprecise or unusual connotation of the word 'distinct'. Reading on...

"Generalize for n distinct weights" => here we use the term 'distinct' again. It's still unclear what the problem statement is. Are we saying there are n different types of coins each with a distinct weight and the task is to deduce the minimum weight coin from a set of m coins (e.g. m == 10). Or, is the question to scale the answer as the set size of coins is increased to n coins (saying nothing about how many different coin types (i.e. distinct weights) there are?

So...
Assume a set comprising C coins total.
The set of C coins comprises coins chosen from another set T whose members are archetypes (e.g. T might comprise pennies, nickles, dimes, quarters). Each member of T has a distinctly different weight.

What is the size of set C and the size of set T? Are there any assumptions about the composition of C given T? Or, is C simply chosen from random from T?

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

we have a scale..take pairs of coins..throw away the heavier ones. recurse..binary search..lg(n).

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

This is same as finding the least number from a set of numbers, compare each ball to the next ball, discard the heavier one and continue with the lighter ball.

I believe the number of comparisons regd is n-1.

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

The answer is n-1.
But proving this is the trickier part.

This is one proof that I would have given. Convert the given problem to a graph problem. The players are treated as nodes and a comparison between 2 players is treated as an edge. Now, the problem boils down to graph connectivity. To have a connected graph, there should be a minimum of n-1 edges. If we have less than n-1 edges, it guarantees that the graph is not connected.

PS: While less than n-1 edges guarantees that the graph is not connected, greater than n-1 edges does not guarantee connectivity. I will leave it to you to figure out why.

- CJ February 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

I think the ans would be log n ( like if n=8, u can keep 4 on one pan and 4 on another pan of the balance. You get the less weighing 4 and again split it into 2 on each pan..finally u compare the two smallest ones by placing one on each pan and you get ur ans.) tell me if there is a better way

- smack March 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

All weights are distinct, please read the question carefully.

- Aryan March 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

All weights are distinct, please read the question carefully.

- Aryan March 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yep sorry...confused it with another question asked from me in an interview

- smack March 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

read the question carefully.
all coins hav diffrent weights.

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

Yup,
this explains that there need to be n-1 trails need to find the winner.

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

Selection algorithm.
This is finding the smallest ( can also generalize it to kth smallest) number in a set of number using a comparator.

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

I also got 9. Weigh 2 different coins each time to get 5 coins from the 10 that weigh less in each weighing, and then repeat with 4 of these 5 coins, and then repeat with the 2. Since there's an odd number, the final coin that was left out will need to be weighed.

Generalizing this method should require a proof by induction since this method relies on using an algorithm. I'm out of time atm, but it should be a simple proof (induct on powers of 2, i.e. if 2^i is the smallest power of 2 contained in n holds, say 2^(i+1) is now the smallest power of 2 and try to see if the hypothesis works). I suspect that the n-1 solution is correct as well, but I haven't worked the details yet.

- Wesley Cho April 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i m not getting that how can be use selection algorithm.
actually weight is not given.\
if weight would be given then we could use quick sort

- Anonymous June 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
-2
of 4 vote

The answer is 4.

This can be derived from: 2^n = 4
2^3 is 8
and 2^4 is 16, so we have to go with n=4.

So we will have following weights:
2^0, 2^1, 2^2, 2^3
That is:
1, 2, 4, 8

Now imagine yourself if you can derive all the numbers between 1 to 10 or not from it.

- Rakesh Kalra May 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Did you understand the question?

- Anshu Kumar July 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

you commented on a differnent questn bro............
u are going nowhere./
:)

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

Let's say you weigh (9kg,8kg,7kg,6kg,1kg) vs (10kg,5kg,4kg,3kg,1kg) , then going by (5,5) theory our lightest coin will be counted out with the heavier group !

- adnanzafar2 July 18, 2012 | 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