Wrox Programmer Forums
|
BOOK Programming Interviews Exposed: Secrets to Landing Your Next Job 3rd Edition
This is the forum to discuss the Wrox book Programming Interviews Exposed: Secrets to Landing Your Next Job, 3rd Edition by John Mongan, Noah Kindler, Eric Giguere; ISBN: 978-1-118-26136-1
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK Programming Interviews Exposed: Secrets to Landing Your Next Job 3rd Edition section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 2nd, 2013, 03:00 PM
Registered User
 
Join Date: Mar 2013
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Lower Common Ancestor Problem (pg 73)

Hi,

I agree 90% with the solution to this problem. However, it is not explicit what the answer would be for the LCA of nodes with value 12 and 14 in the picture. The implementation suggested would place 12 as the solution.

It seems 8 would be a more fitting LCA, so I stored the previousNode for each iteration. If the two are not both greater or both less than, then I check for if the currentNode is A or B. If so, then use previousNode (convergence). Otherwise currentNode is LCA(divergence).

Thoughts?

Erich
 
Old April 3rd, 2013, 06:37 AM
Wrox Author
 
Join Date: Oct 2012
Posts: 10
Thanks: 0
Thanked 1 Time in 1 Post
Default

It all hinges on what you consider to be the definition of "ancestor". If a node is an ancestor of itself (which is what the definition in the book implies) then the book's assertion is correct. If you say that a node cannot be its own ancestor, then your assertion is correct.

But if a node is not an ancestor of itself, you'll have problems dealing with finding the LCA of any set of nodes that includes the root node....

Eric
 
Old April 3rd, 2013, 06:52 AM
Wrox Author
 
Join Date: Oct 2012
Posts: 10
Thanks: 0
Thanked 1 Time in 1 Post
Default

The point I wanted to make was that in an interview you should make it clear what you mean by "ancestor". In your scenario, for example, returning null would make the most sense for any set of nodes that included the root node. But the interviewer might be confused unless you told them what your interpretation of "ancestor" was.

Eric
 
Old April 3rd, 2013, 11:55 PM
Registered User
 
Join Date: Mar 2013
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

that's an excellent point! i did not consider a pair of nodes including the root node.... should have saw that edge case :P thanks for the reply!
 
Old April 4th, 2013, 06:38 AM
Wrox Author
 
Join Date: Oct 2012
Posts: 10
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
Originally Posted by erichlin View Post
that's an excellent point! i did not consider a pair of nodes including the root node.... should have saw that edge case :P thanks for the reply!
No problem, and thanks for the question!

Eric





Similar Threads
Thread Thread Starter Forum Replies Last Post
coding problem in common dialog box cancertropica Visual Basic 2005 Basics 0 July 30th, 2008 09:38 AM
parent/ancestor nmnm XSLT 1 April 24th, 2007 05:25 AM
Common question, not so common answer? flyin ADO.NET 5 March 24th, 2004 06:50 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.