Nitin Agrawal
Contact -
  • Home
  • Interviews
    • InterviewFacts
    • Resume Thoughts
    • Companies >
      • InvestmentBanks >
        • ECS
        • Bank Of America
        • WesternUnion
        • WellsFargo
      • ProductBasedCompanies >
        • CA Technologies
        • Model N India
        • Verizon Media
        • Oracle & GoJek
        • IVY Computec
        • Samsung
        • ClearWaterAnalytics
        • ADP
        • ServiceNow
        • Pubmatic
        • Expedia
        • Amphora
        • CDK Global
        • Delphix
        • Fractal Enterprises LLP
        • CDK Global
        • Tide-Banking
        • Epic
      • ServiceBasedCompanies >
        • ASG World Wide Pvt Ltd
        • Paraxel International & Pramati Technologies Pvt Ltd
        • MitraTech
        • Intelizest Coding Round
        • ZeMoSo
    • Interviews Theory
  • Programming Languages
    • Java Script >
      • Tutorials
      • Code Snippets
    • Reactive Programming >
      • Code Snippets
    • R
    • DataStructures >
      • LeetCode Problems
      • AnagramsSet
    • Core Java >
      • Codility
      • Java14
      • Threading >
        • ThreadsOrder
        • ProducerConsumer
        • Finalizer
        • RaceCondition
        • Executors
      • Important Points
      • Immutability
      • Dictionary
      • Sample Code Part 1 >
        • PatternLength
        • Serialization >
          • Kryo2
          • JAXB/XSD
          • XStream
        • MongoDB
        • New methods in Collections
        • MethodReferences
        • Complex Objects Comparator >
          • Performance
        • NIO >
          • NIO 2nd Sample
        • Date Converter
        • Minimum cost path
        • Find File
      • URL Validator
    • Julia
    • Python >
      • Decorators
      • String Formatting
      • Generators_Threads
      • JustLikeThat
    • Go >
      • Tutorial
      • CodeSnippet
      • Go Routine_Channel
      • Suggestions
    • Methodologies & Design Patterns >
      • Design Principles
      • Design Patterns >
        • Decorator
        • Proxy
        • Lazy Initialization
        • CombinatorPattern
        • RequestChaining
        • Singleton >
          • Singletons
  • Frameworks
    • AngularJS
    • Apache Velocity
    • Spring >
      • Spring Boot >
        • Issues
      • Quick View
    • Rest WebServices >
      • Interviews
      • Swagger
    • Cloudera BigData >
      • Ques_Ans
      • Hive
      • Apache Spark >
        • SparkCode
        • Sample1
        • DataFrames
        • RDDs
        • SparkStreaming
        • SparkFiles
    • Integration >
      • Apache Camel
    • Testing Frameworks >
      • JUnit >
        • JUnit Runners
      • EasyMock
      • Mockito >
        • Page 2
      • TestNG
    • Blockchain >
      • Ethereum Smart Contract
      • Blockchain Java Example
    • Microservices >
      • Design Patterns
    • AWS >
      • Honeycode
  • Databases
    • Oracle >
      • Interview1
      • SQL Queries
    • Elastic Search >
      • StudySources
  • Random issues
    • TOAD issue
    • Architect's suggestions
    • ApacheSpark Installation
  • Your Views

​LCA(LeastCommon Ancestor)

Most of you guys might have seen the problem about finding the LCA(LeastCommon Ancestor) for the 2 nodes given in a tree.
Note : Here I am taking ancestor as 'Ancestor' only & as per me no node can be its own ancestor, no matter what theory is                 given for this.
So below I present one solution to find the correct LCA, plus it is applicable for any number of nodes for which one
wants to find the LCA. Also if any one of the given nodes is not present in the tree, then it will give the message that all
the given nodes are not present, else will give the correct LCA.
Please try once & let me know about your thoughts on this.
You can refer below tree to test the below code or you can create your own using the code given below.
Picture
Picture
Now integrate the below code with the above to execute & test the code against the tree given above. Change the test values as per the requirements.
Picture
So above I have used 2 versions of LCA(). LCA1() is the implementation of the idea like shown across internet to find the common Ancestor & LCA() is the implementation of my understanding of the word 'Ancestor'. So check the capabilities of both the methods. Using my method (1) you will be giving the correct 'Ancestor' (2) It will check if both the nodes present then only a valid 'Ancestor' can exist. (3) This method can be used to find the 'Ancestor' of multiple nodes.
Assumption :- All nodes in the given tree must be unique.
Picture
Below is the implementation which you see across the internet which is wrong as per my understanding of the word 'Ancestor'. It is also wrong in the sense that if one node is not present then it will return the present node as the answer. Plus it has unsaid assumption that both the nodes must be present in the tree.
Picture
Try both the above methods & check the difference. Please let me know if you see any issue.
But I still wonder about how such wrong understanding is being used in multiple interviews of even big companies to select the candidates where the interviewers themselves don't understand the basic concepts.
Powered by Create your own unique website with customizable templates.