Dividing Game Interview Coding Sample

I was recently given the challenge to write an solution for the Dividing Game.  The objective Player 1 and Player 2 each choose a number.  The solution should output the number of common divisor’s they share in common between the selected numbers.

This logic is rather simple, create a list of known divisors for each number, then use the list to only retain the contents that are common to both lists.  Then you have your common divisors between the two numbers.

import java.util.ArrayList;
 
/**
 * @author jmiller Class to play the dividing game
 */
public class DividingGame
{
    /**
     * Start the Dividing Game
     *
     * @param playerOne
     *            Player 1 Chosen Number
     * @param playerTwo
     *            Player 2 Chosen Number
     * @param divisor
     *            Number to divide by
     */
    public DividingGame(int playerOne, int playerTwo, int divisor)
    {
        System.out.println("playerOne = " + playerOne + " / playerTwo = " + playerTwo);
        System.out.println("Returns: " + this.getNumber(playerOne, playerTwo, divisor));
    }
 
    /**
     * Get the divisors for the number
     *
     * @param value
     *            Number to divide
     * @param divisor
     *            Divide the number by this
     * @return Array containing the values by the divisor
     */
    private ArrayList<Integer> getDivisor(int value, int divisor)
    {
        ArrayList<Integer> list = null;
 
        // If we are not even with the divisor we are done.
        if ((value % divisor) > 0)
        {
            list = new ArrayList<Integer>();
            list.add(new Integer(value));
        }
        else
        {
            list = this.getDivisor(value / divisor, divisor);
            list.add(new Integer(value));
        }
 
        return list;
    }
 
    /**
     * Determine the number of common divisors
     *
     * @param playerOne
     *            Player One picked Number
     * @param playerTwo
     *            Player Two picked Number
     * @param divisor
     *            Divide the number by this
     * @return Number of common divisors
     */
    public int getNumber(int playerOne, int playerTwo, int divisor)
    {
        // Get the list of divisor's for each number
        final ArrayList<Integer> oneList = this.getDivisor(playerOne, divisor);
        final ArrayList<Integer> twoList = this.getDivisor(playerTwo, divisor);
 
        // Only contain the common elements
        oneList.retainAll(twoList);
 
        // Return the size of the common elements
        return oneList.size();
    }
}
Ghostprogrammer
  • Ghostprogrammer
  • I am known for being able to quickly decipher difficult problems to assist development teams in producing a solution. I have been called upon to be the Team Lead for multiple large-scale projects. I have a keen interest in learning new technologies, always ready for a new challenge.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: