3

Sometimes I come across interview questions like this: "Find the common parent of any 2 nodes in a tree". I noticed that they ask LCA questions also at Google, Amazon, etc.

As wikipedia says LCA can be found by intersection of the paths from the given nodes to the root and it takes O(H), where H is the height of the tree. Besides, there are more advanced algorithms that process trees in O(N) and answer LCA queries in O(1).

I wonder what exactly interviewers want to learn about candidates asking this LCA question. The first algorithm of paths intersection seems trivial. Do they expect the candidates to remember pre-processing algorithms ? Do they expect the candidates to invent these algorithms and data structures on the fly ?

Michael
  • 41,026
  • 70
  • 193
  • 341
  • its not trivial question if u dont know it already. restrictions such as not allowed to store parent and storing additional nodes for "general" tree makes it bit difficult. I flunked my interview bcoz of this question :( – Pranalee Feb 18 '13 at 15:25

2 Answers2

9

They want to see what you're made of. They want to see how you think, how you tackle on problem, and handle stress from deadline. I suppose that if you solve the problem because you already know the solution then they will just pose you another problem. It's not the solution they want, it's your brainz.

Dialecticus
  • 16,400
  • 7
  • 43
  • 103
  • 1
    +1, but @David, It's not just for IQ test, they will see your manner in hard situation and your re action when new problem comes to you. – Saeed Amiri Dec 26 '10 at 17:36
2

With many algorithms questions in an interview, I don't care (or want) you to remember the answer. I want you to be able to derive the answer from the main principles you know.

Realistically: I could give two cares less if you already know the answer to "how to do X", if you are able to quickly construct an answer on the fly. Ultimately: in the real world, I can't necessarily assume you have experience tackling problems of domain X, but if you run across a problem in said domain, I will certainly hope you'll have the analytical ability to figure out a reasonable answer, based on the general knowledge you hold.

A tree is a common data structure it's safe to assume most will know - if you know the answer offhand, you should be able to explain it. If you don't know the answer, but understand the data structure, you should be able to fairy easily derive the answer.

James
  • 8,512
  • 1
  • 26
  • 28
  • I concur that it is pretty easy to derive O(H) solution. However it is pretty difficult (IMO) to derive the pre-processing solution. So, I wonder if one expect candidates to derive it. – Michael Dec 30 '10 at 16:57
  • @Michael I think all of this applies more to the case where you don't have parent pointers. That makes it more suitable for the process James is describing. – keyser Sep 27 '14 at 16:43