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..!

ZOHO QUESTION : Given a 9×9 sudoku we have to evaluate it for its correctness. We have to check both the sub matrix correctness and the whole sudoku correctness.

Discussion and Analysis:

Here is some interesting question we have about checking sudoku correctness.

Here the rules of sudoku following,

For example we have 9*9 sudoku,

So we have 81 number of square grid. This grid divided into 9 blocks.Each block have 9 square so totally 81 square.

So it has 9 rows and 9 columns. Each rows have number 1-9. But a number appears only once in the row as well as same rule for column.


Solution : 


/**
     * Find correctness.
     *
     * @return true, if successful
     */
    private static boolean evaluateSudoku() {
//Here is our input as 9*9 matrix.
        int[][] l_arrSudukoMatrix = new int[][] {
                { 5, 1, 3, 6, 8, 7, 2, 4, 9 },
                { 8, 4, 9, 5, 2, 1, 6, 3, 7 },
                { 2, 6, 7, 3, 4, 9, 5, 8, 1 },
                { 1, 5, 8, 4, 6, 3, 9, 7, 2 },
                { 9, 7, 4, 2, 1, 8, 3, 6, 5 },
                { 3, 2, 6, 7, 9, 5, 4, 1, 8 },
                { 7, 8, 2, 9, 3, 4, 1, 5, 6 },
                { 6, 3, 5, 1, 7, 2, 8, 9, 4 },
                { 4, 9, 1, 8, 5, 6, 7, 2, 3 } };
        Set<Integer> l_stRowSet = new HashSet<Integer>();
        Set<Integer> l_stColumnSet = new HashSet<Integer>();
//Here haveDuplicate variable to return the true/false. If haveDuplicate is false the sudoku is valid one.Otherwise its wrong sudoku.
        boolean haveDuplicate = true;

        for (int i = 0; i < l_arrSudukoMatrix.length; i++) {
            for (int j = 0; j < l_arrSudukoMatrix[i].length; j++) {
//Here to check the entered value is greater than zero and less than 10
              if(l_arrSudukoMatrix[i][j] > 0 && l_arrSudukoMatrix[i][j] < 10){
                l_stRowSet.add(l_arrSudukoMatrix[i][j]);
                }
            }
            for (int j = 0; j < l_arrSudukoMatrix[i].length; j++) {
//Here to check the entered value is greater than zero and less than 10
              if(l_arrSudukoMatrix[j][i] > 0 && l_arrSudukoMatrix[j][i] < 10){
                l_stColumnSet.add(l_arrSudukoMatrix[j][i]);
                }
            }
//If Row/Column have less than 9 size we have some repeated value, so we can return true.
            if (l_stRowSet.size() != 9 && l_stColumnSet.size() != 9) {
                return false;
            }
        }

        return haveDuplicate;

    }

Output : For this given input matrix true will be return.
If you modify some input with repeated value or greater than 9 and less than zero then system return false.


NOTE: Please share your comments to improve the flag. Thank you!

Monday, September 25, 2017

ZOHO Question : Find sum of weights based on the following conditions

Question :

Given a set of numbers like <10, 36, 54,89,12> we want to find sum of weights based on the following conditions
    1. 5 if a perfect square
    2. 4 if multiple of 4 and divisible by 6
    3. 3 if even number

And sort the numbers based on the weight and print it as follows

<10,its_weight>,<36,its weight><89,its weight>.

Solution : 
      
              To solve this program we need some logic to find the give conditions,
  • Weight is 5 if given number is perfect square of any number for example : 4,9,16,36
  • Weight is 4 if given number multiple of 4 AND divisible by 6
  • Weight is 3 if given number is even number. 
Some number may pass any two of the above condition, some of them pass single condition.

Program solution:

Condition 1 : Weight is 5 if given number is perfect square of any number for example : 4,9,16,36

 /**
     * Check squere.
     *
     * @param number the number
     * @return the int
     */
    private static int checkSquere(int number) {
        int l_ireturn = 0;
            for (int i = 2; i < number; i++) {
                int j = number / i;
                if (i == j && i*j == number) {
                    l_ireturn = i;
                }
            }
            return l_ireturn;
    }

 Condition 2 : Weight is 4 if given number multiple of 4 AND divisible by 6

/**
     * Logic multiple divison.
     *
     * @param number the number
     * @return the int
     */
    private static int logicMultipleDivison(int number){
        int l_ireturn = 0;
        int l_iMulti = number/4;
        if(l_iMulti*4 == number)
        {
            l_iMulti = number/6;
            if(l_iMulti*6 == number)
            {
                l_ireturn = 4;
            }
        }
       
        return l_ireturn;
    }

Condition 3 : Weight is 3 if given number is even number. 

/**
     * Logic even number.
     *
     * @param number the number
     * @return the int
     */
    private static int logicEvenNumber(int number)
    {
        int l_ireturn = 0;
       
        if(number%2 == 1)
        {
             l_ireturn = 3;
        }
        return  l_ireturn;
    }

Do you think i have missed something from the question? . yes we missed it but we have the coding solution below?


In the question we have set of numbers are given as input so we need to iterate all the numbers from given set and check the above condition 

Do Iterate the set of given number and find the weight of each number by calling the logic what we have done before..!

private static Map<Integer,Integer> doIterateSet(Set<Integer> l_setInteger)
    {
        Iterator<Integer> itr = l_setInteger.iterator();
        Map<Integer,Integer> l_mapWeightMap = new HashMap<Integer,Integer>();
        while (itr.hasNext()) {
            int number = itr.next();
            int l_iSquere = checkSquere(number);
            int l_iMulDiv = logicMultipleDivison(number);
            int l_iEven = logicEvenNumber(number);
            l_mapWeightMap.put(number, l_iSquere+l_iMulDiv+l_iEven);
        }
        return l_mapWeightMap;
    }


Yes, We are iterate the set but where is the set input, Cool we have the main method, which have the set of integer input...

 public static void main(String[] args) {
      
        Set<Integer> l_setNumberSet = new HashSet<Integer>();
        l_setNumberSet.add(10);
        l_setNumberSet.add(36);
        l_setNumberSet.add(54);
        l_setNumberSet.add(89);
        l_setNumberSet.add(12);
        Map<Integer,Integer> l_mapWeightMap = doIterateSet(l_setNumberSet);
        System.out.println(l_mapWeightMap);
      

    }

Finally the println method prints the given number and there weights for given condition.

Output : 

{54=0, 36=10, 10=0, 89=3, 12=4}

Here i just print the map as output if you want some readable format please do the the change on the main method and iterate the map and print the output whatever you want. 
 

 NOTE : Please give your comment to improve my coding standard as well as my functional logics.

Friday, September 22, 2017

Find the sentance is pangrams or not using java?

First Pangrams explanation:
 
Pangrams are sentences constructed by using every letter of the alphabet at least once.


 Here is java code to find the pangrams,


private static void findPangrams(String p_sSentance) {
        Set<Character> l_objSet = new HashSet<Character>();
        int l_iLength = p_sSentance.length();
        while(l_iLength > 0)
        {
            if(p_sSentance.charAt(l_iLength-1) != ' '){
            l_objSet.add(p_sSentance.charAt(l_iLength-1));
            }
            l_iLength--;
        }
        if(l_objSet.size() == 26)
        {
            System.out.println("Pangram");
        }else{
            System.out.println("Not pangram");
        }
    }

Input 1 : "Pack my box with five dozen liquor jugs"
Input 2 : "Pack my box with five liquor jugs"

Output 1 :  Pangram
Output 2 :  Not pangram