Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Identify all Grand-Parent Nodes of each Node in a Map

  • Last Updated : 17 May, 2017

Given in input that has relationships between a person and their children, for all the people in this data, identify grandparents of all the people in the input.

Input: A map of all the people and their children
Map[String, Set[String]]

Output: A map of all people and their grandparents
Map[String, Set[String]]

Map(A -> Set(B,C), B -> Set(D, C), C -> Set(E))

Map(D -> Set(A), C -> Set(A), E -> Set(A, B)) 

We strongly recommend you to minimize your browser and try this yourself first

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Here we have iterate over each and every node in Map and find out the grand-parent of each child.

The idea is to use Recursion. But, it can be solved without Recursion too.

Lets see the Without Recursion solution first:

Solution 1 (Without Recursion):

Here we have to use Mutable Scala Map. Below is the Scala Code.

val input = Map("A" -> Set("B","C"), "B" -> Set("D", "C"), "C" -> Set("E"))
val output: scala.collection.mutable.Map[String, Set[String]]
  = scala.collection.mutable.Map() => =>
    input.get(child).map(grandchildren =>{grandchild =>
        if(output.keys.exists(_ == grandchild)) {
          output.put(grandchild, output.get(grandchild).get ++ Set(node._1))
        } else {
          output.put(grandchild, Set(node._1))

Here we are iterating over every Node of Map and finding out the grandchildren of each child in node.This, will help us in creating a Map with Grand-Child and Grand-Parents relationship.

Solution 2 (With Recursion):

Here we can use Immutable Scala Map as we are using Recursion.

val input = Map("A" -> Set("B","C"), "B" -> Set("D", "C"), "C" -> Set("E"))
val output = findGrandparents(input)

  def findGrandparents(family: Map[String, Set[String]]): Map[String, Set[String]] = {
    family.foldLeft(Map[String, Set[String]]()){
      case (grandParents, oneFamily) => {
        val grandChildren: Set[String] = oneFamily._2.flatMap(member => family.get(member)).flatten
        val res = => {
          grandParents.get(child) match {
            case None =>(child -> Set(oneFamily._1))
            case Some(x) => (child -> (x + oneFamily._1))
        grandParents ++ res

This article is contributed by Himanshu Gupta. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!