Yahoo Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
9
of 11 vote

Ok heres the solution. SantiagoYMG is pretty close.
The solution is O(n) in time complexity.

Make all of them stand in a row.
Lets say the people are a,b,c,d,e,f,g,h,i,j,.......n

Compare a and b.
if a knows b => a is certainly not the celebrity. Probable celebrity = b
if a doesnt know b => b is certainly not the celebrity. Probable celebrity = a

In either case compare the probable celebrity to the next person in line ie 'c' and repeat the process. Each comparison should eliminate 1 person and have another as the probable celebrity. At the end, the probable celebrity who survives is the certain celebrity.

Complexity = O(n)

- sreemana@buffalo.edu March 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is not correct might be a and b both dont know each other then you cant exclude both.

- Santosh March 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Valid point santosh but you fail to understand the logic here. We are not looking at two people knowing each other, we are interested in knowing if one knows the other or not.(a knows b doesnt mean b knows a).

If a and be dont know each other, they both will eventually get eliminated. Try this out, trust me it works!

- sreemana@buffalo.edu March 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Well, that's a good algorithm if you have the full (N+1)x(N+1) matrix of the "knows" relationship.
In that case the "a knows b" test has O(1) complexity.
If you instead have, for each a, an array of the persons known by a, the test would be a look up function, with complexity (in the worst case of unsorted array) O(N). This gives an overall complexity of O(N^2).

In this case, assuming that isEmpty(array) is a O(1) test, you can still have O(N) complexity but the algorithm would be pretty trivial: test all the persons until you find the empty list.

- david.rebatto@gmail.com March 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Well, that's a good algorithm if you have the full (N+1)x(N+1) matrix of the "knows" relationship.
In that case the "a knows b" test has O(1) complexity.
If you instead have, for each a, an array of the persons known by a, the test would be a look up function, with complexity (in the worst case of unsorted array) O(N). This gives an overall complexity of O(N^2).

In this case, assuming that isEmpty(array) is a O(1) test, you can still have O(N) complexity but the algorithm would be pretty trivial: test all the persons until you find the empty list.

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

Sorry for double posting. It seemed to refuse the comment so I thought it needed a real e-mail address...

- david.rebatto@gmail.com March 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This one is correct, I was asked the same question in a phone interview with Google.

The key point is to use elimination. In the end, there will be only one candidate left. Then check if it is a true celebrity.

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

This one is correct, I was asked the same question in a phone interview with Google.

The key point is to use elimination. In the end, there will be only one candidate left. Then check if it is a true celebrity.

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

@sreemana
I agree a knows b does not mean b knows a. But a knows b also does not mean that b does not know a. Let say a knows b we exclude a. now our probablity is b.
Now consider there are only 3 peapole a b c and let say c is celebrety
with the follwing relation
a->b. i.e a knows b
b->a
b does not know c.
c does not know a and b.

now can you expain how your algo will work. I am little bit confuse. I might be wrong but need some more clarification.

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

@sreemana


Cool dude finally got the logic :). Thanks for posting

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

Santosh, Read the question.
If c is the celebrity, the possibility of b not knowing c is invalid.

- sreemana@buffalo.edu March 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yup man got it later, apologies for misunderstanding....

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

the coolest one

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

It looks like your assuming that one person gets eliminated in every comparison. Then, maybe we have O(n). Assume that everyone in the room do not know each other, and each person knows only one person, the celebrity. What would you do? In other words, what if you cannot eliminate
a,b,c,d,e,f,g,h,i,j on comparison and
'n' is the celebrity?

My solution would be a quicksort like technique, with each iteration pushing people into categories 'knows the person', 'does not know the person' where the person under comparison is like the pivot element. After an iteration we are interested only in the partition that contains people who don't know the pivot point

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

Gosh!!! Why don't you guys read the question properly.
If nobody knows anybody else other than the celebrity it becomes all the more easier.

If a doesnt know b (b defn is not the celeb)
and b doesnt know a (a defn is not the celeb)

Then a and b both aren't celebrities. You can always eliminate people. READ THE QUESTION CAREFULLY!

- sreemana@buffalo.edu June 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Gosh!!! Why don't you guys read the question properly.
If nobody knows anybody else other than the celebrity it becomes all the more easier.

If a doesnt know b (b defn is not the celeb)
and b doesnt know a (a defn is not the celeb)

Then a and b both aren't celebrities. You can always eliminate people. READ THE QUESTION CAREFULLY!

- sreemana@buffalo.edu June 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ok, got it. Thanks.

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

Hi this is good solution, but the time complexity should not be O(n), it should be O(nlogn), coz you cannot guarantee you can eliminate n-1 in the first round, the worst situation you can only delete n/2.

- shiqing881215 September 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

:) :)

- Bevan November 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The O(n) solution where we keep them in a row and compare the current possible candidate with next person in row to eliminate either one of them or both of them as a candidate (posted by sreemana@buffalo.edu) works but with one catch.

After we do the first pass, if we are left with one possible candidate, we need to do another pass to confirm that he is indeed the celebrity. (that everyone else, to be precise, everyone else before him in the row know this person and he doesn't know any of those persons). We can return that candidate only after that check otherwise there is no celebrity.

Let me explain with an example: Let's say we have persons a,b,c,d and here are the relationships: a knows b, c knows b, b knows d, d knows a (other than these all other possible pairs are "does not know" case), in this scenario, at the end of the loop, we will have 'd' as celebrity but he is really not.

- pradeep.v.kadubandi October 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Take any person A let it be set S {a1,a2,a3};
Go to each person ax in set S.
if the ax has a know person he is not a celebrity.
else if ax knows none, then he is celebrity.



This methods can be done in time o(n). this is when the set S is considerably large ~n.

in an average case it should be of constant order.

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

if the ax knows none how can you say he is celebrity...according to question non celebrity may/may not know anyone in the room.
ax can be non celebrity also.

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

Celebrity dont know anyone, but non-celebrity should atleast know the celebrity(questions says everyone knows the celebrity)

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

Exactly. Someone is a celebrity iff they know no one. That's because any non-celebrity would know at least the celebrity.

- eugene.yarovoi March 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

suppose the celebrity is at last and assume every non-celebrity knows no one except celebrity,now it takes O(n2) time for finding celebrity,but not O(n).

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

plz read the comments for the correct solution before posting your's..Not to be rude or anything, but im tired of having to explain time and over again how the solution is O(n) for absolutely all situations!

- sreemana@buffalo.edu June 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This algo. is no doubt O(n),

- Water July 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Here is my Algo.
Asumption : Each person will have information with him as follows. Let's say xyz is person then it will have info structure like {xyz : a, b, c,e} it means xyz knows a. b, c, e persons.

1. Draw a directed graph where a -> b means a knows b. Along with two extra variables no. incoming edges and no. out going edges.
2. While drawing the graph increase these variables when you add one relation in graph.
3. Now just iterate over the graph and find the point where incoming variable count is n-1 and outgoing count is 0.

Compexity :
1. For drawing the graph O(n)
2. For finding the max no. incoming edges and outgoing edges O(n)
3. total = O(n)+O(n) = 2O(n) = O(n). as 2 is const. we can exclude it

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

I think this is an overly complicated solution to a simple problem.

A lot of the details you've mentioned overlooks the coding complexity involved. Heres how:

1) Lets say Im making my graph, before I even start constructing the graph I(as a coder) need to all information about who knows whom. This information can get stored in a confusion matrix. So if I need to code this, I would have an extra memory requirement to store this information.

2) When you talk about iterating through the graph, you need to ensure that given a particular node, you should be able to reach every other node. What happens if no individual knows anybody else in the room other than the celebrity. How do you iterate through your graph then?

3) Considering directed graphs, you need to consider the possibility of having loops in them. If they do, you could easily end up in a typical infinite recursion scenario. Sure, you could maintain a visited flag variable for each node, but that again adds to your memory. Not asymptotically, but practically for small n.

4) If I give you this problem to solve in place, your graph method wont work.

5) My solution when implement in code, requires just one fuction bool knows(a,b) which simply returns true if a knows b and false otherwise. Your solution requires way more.

Your solution is valid, but you need to take the above points into consideration. As I said, this problem looks for an efficient algorithm, not your data structure manipulation skills.

Thanks.

- sreemana@buffalo.edu March 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@sreemana

I agree, thank you for pointing this out

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

What kind of traversal algorithm you can use. guess this will end into infinite loop.

- ask4prasath July 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

We represent the people in the room as: a, b, c, d, ..., n
Stand these people in a line.
Ask the first person who all they know.
If the first person knows no one, then they are the celebrity.
If they know 5 people, then the celebrity is one of those 5 people.
Suppose a knows (b, c, d, e, f). Now, out of these 5 people, we only need to find the person who doesn't know anyone. The worst case complexity of this algorithm is O(n) which is when the first person we ask knows everyone. Then we have to loop through the n people to find out who doesn't know anyone. But if the first person knows only m people (such that m<= n), then our algorithm will run in O(m).
In our example, we will only need to find out who knows no one out of the 5 people that a knows.
Someone correct me if my logic is incorrect please.

- Bonzo June 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Doesn't the process of finding if someone knows anyone or not take O(n)? If it does, then does your algorithm not fail to be a linear time running algo?

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

The question does not say anything about the complexity of asking someone who all they know.
But you're right, if it does take O(n) time, then my algorithm will not run in linear time.

- Bonzo June 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

The O(n) solution where we keep them in a row and compare the current possible candidate with next person in row to eliminate either one of them or both of them as a candidate (posted by sreemana@buffalo.edu) works but with one catch.

After we do the first pass, if we are left with one possible candidate, we need to do another pass to confirm that he is indeed the celebrity. (that everyone else, to be precise, everyone else before him in the row know this person and he doesn't know any of those persons). We can return that candidate only after that check otherwise there is no celebrity.

Let me explain with an example: Let's say we have persons a,b,c,d and here are the relationships: a knows b, c knows b, b knows d, d knows a (other than these all other possible pairs are "does not know" case), in this scenario, at the end of the loop, we will have 'd' as celebrity but he is really not.

- pradeep.v.kadubandi October 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

Guys this a pretty cool algorithmic problem. Give it a shot.

- sreemana@buffalo.edu March 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, I think this is the right answer.

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

fucck u

- Anonymous June 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Suppose that we use a directed graph (not DAG cause we can have cycles). And graph represented as an adjacency list. Go though all vertexes and find first one which doesn't contains any edges. Time: O(V) Space: O(V+E)

1->3->2->6
2->6->5
3->5->6
4->6->3
5->6
6-> EMPTY

So "6" is celebrity.

- m@}{ March 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The problem can posed as UniversalSink problem as explained in CLR's Algorithms book (Cormen). Isnt it?

- Mr.Unknown April 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

^ Good luck for your interviews buddy!

- sreemana@buffalo.edu May 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

^Oops sorry that sarcasm wasn't meant for you!

Yet to check out the Universal Sink solution though!

- sreemana@buffalo.edu May 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If everyone knows the celebrity, why not just ask one of them who the celebrity is?? No algorithm involved in this. Since everyone knows the celebrity, they will mention who he is. If the first person you approach does not know who the celebrity is, then he is the celebrity.

- shivmail89 May 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

^ Good luck for your interviews buddy!

- sreemana May 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ha ha, thanks for your condescension..

- shivmail89 May 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Does a person known to himself, including the celebrity?

- Anonymous June 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

this is fairly a simple problem can be solved on O(n) complexity

condider a set of people as a1,a2,a3... an and say an+1 is a celebrity..

randomly pick any one person say ak and check how many people it know..
if number of people known is 0 then ak is the celebrity else someone from the people known by ak is the celebrity..
now check this on all the people known by ak

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

definitely the best algo

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

Cool. Yes, you need to make 'n' checks to see if ak is known. O(n) is worst as well as best case.

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

Take any person and get all the peoples he knows.

Consider there 1..n people and person 1 knows k people (k <= n)
then we just have to iterate the k people and return the person who doesnt know anybody.

This will solve the problem in O(k + 1)
The worst case will be O(n) if person knows all the people.

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

Make them to stand in a line and give numbers to them like 1 to n .

Use Binary search here , ask n/2 guy to find if n/4 or 3n/4 is a celebrity , if not ask them to do the same thing

- Aditya Pn February 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// DFS ?
class Person{
	private String adharId;
	private Set<Person> circle;
	public Person(String adharId) {
		this.adharId = adharId;
		this.circle = new HashSet<Person>();
		// TreeSet<Person>(); //if you want to build relationship depth
	}
	public void addToCircle(Person person){
		circle.add(person);
	}
	public String getDetails(){
		return this.adharId;
	}
	public int getNoOfFriends(){
		return this.circle.size();
	}
}
public Person getCeleb(Map<Person, Integer> room){
	//iterate thorough the values and return the poor man having 0 friends
	return null;
}
Map<Person, Integer> room = new HashMap<Person, Integer>();

- Ashis Kumar March 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// DFS ?
class Person{
	private String adharId;
	private Set<Person> circle;
	public Person(String adharId) {
		this.adharId = adharId;
		this.circle = new HashSet<Person>();
		// TreeSet<Person>(); //if you want to build relationship depth
	}
	public void addToCircle(Person person){
		circle.add(person);
	}
	public String getDetails(){
		return this.adharId;
	}
	public int getNoOfFriends(){
		return this.circle.size();
	}
}
public Person getCeleb(Map<Person, Integer> room){
	//iterate thorough the values and return the poor man having 0 friends
	return null;
}
Map<Person, Integer> room = new HashMap<Person, Integer>();

- Ashis Kumar March 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about we use people as nodes ( celebrity too ) and perform DFS on this graph. The node with the EARLIEST finish time will be the sink node .. (note that a sink node has no outgoing edges and hence the earliest finish time), The sink node will be the celebrity. This can be performed in O(n) time.

- Anonymous January 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Shouldn't the worst case complexity be approxO(n2). When total number of comparisons is: (n2-n)/2. Consider the case when the celebrity is at the end of the row. And except everyone (other than celebrity) don't knows each other except celebrity. In a,b,c,d,e. If e is celebrity and if we start from a:
Comparisons will be:
a-b
a-c;b-c
a-d,b-d,b-c
a-e,b-e,c-e,d-e

- avi February 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What if the condition "the celebrity knows no one" wasn't there? Is there any possibility to solve this in O(n) ? I have a similar problem but without that condition.

- Alex December 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

find the node in the directed graph with the most inward coming edges and no outgoing edges

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

can u please tell the time complexity.... i think so it is O(n^2)...

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

sure, because drawing graphs and finding the node with the maximum number of incoming nodes is what people do when a celebrity is in a room right!!!!

- sreemana@buffalo.edu March 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@sreenana
Well, no one really writes algorithms to find celebrities either. So according to you, the question itself is pointless.
Answer the question if you know how a solution. Stop being sarcastic.

- Bonzo June 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

it can be done with n^2 complexity ...but seriously doubt o(n) ..

- Anonymous March 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

well , it's quiet simple, if all of the persons knows the celebrity in the room , the one left behind is the celebrity i guess

- Junaid March 26, 2012 | 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