Tuesday, September 26, 2017

ZOHO QUESTION :Find his no of grandchildren of given person.

Question: Given a two dimensional array of string like

  <”luke”, “shaw”>
  <”wayne”, “rooney”>
  <”rooney”, “ronaldo”>
  <”shaw”, “rooney”>

Where the first string is “child”, second string is “Father”. And given “ronaldo” we have to find his no of grandchildren Here “ronaldo” has 2 grandchildren. So our output should be 2.


Discussion and Analysis :

Do find grandchildren, we need to find the son of ronaldo. Then need to find the son of the ronaldo's son.

First find the number of son ronaldo have, get this as list.


/**
     * Find grand son.
     *
     * @param a
     *            the a
     * @param l_sPerson
     *            the l s person
     * @param l_sSon
     *            the l s son
     */
    private static void findGrandSon(String[][] a, String l_sPerson,
            String l_sSon) {
        List<String> l_lstGrandSon = new ArrayList<String>();
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                if (l_sSon.contains(a[i][j])) {
                    if (j != 0) {
                        l_lstGrandSon.add(a[i][j - 1]);
                        System.out.println(a[i][j - 1] + " is grand son of  "
                                + l_sPerson + " son of " + l_sSon);
                    }
                }
            }

        }
    }


Then do find the son of the ronaldo's son.  

/**
     * Find grand son.
     *
     * @param a
     *            the a
     * @param l_sPerson
     *            the l s person
     * @param l_sSon
     *            the l s son
     */
    private static void findGrandSon(String[][] a, String l_sPerson,
            String l_sSon) {
        List<String> l_lstGrandSon = new ArrayList<String>();
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                if (l_sSon.contains(a[i][j])) {
                    if (j != 0) {
                        l_lstGrandSon.add(a[i][j - 1]);
                        System.out.println(a[i][j - 1] + " is grand son of  "
                                + l_sPerson + " son of " + l_sSon);
                    }
                }
            }

        }
    }

And finally main method with array input./**
     * The main method.
     *
     * @param args
     *            the arguments
     */
    public static void main(String[] args) {
        String[][] a = { { "luke", "shaw" }, { "wayne", "rooney" },
                { "rooney", "ronaldo" }, { "shaw", "rooney" } };

        String l_sPerson = "ronaldo";
        String l_sSon = findSon(a, l_sPerson);
        findGrandSon(a, l_sPerson, l_sSon);
    }

Output:

rooney is son of  ronaldo
wayne is grand son of  ronaldo son of rooney
shaw is grand son of  ronaldo son of rooney




NOTE : Please comment below it will help me to improve the solution. Thank you..!

No comments:

Post a Comment