Solving “Duck Duck Goose” in Java

The challenge

The objective of Duck, duck, goose is to walk in a circle, tapping on each player’s head until one is chosen.

Task: Given an array of Player objects (an array of associative arrays in PHP) and an index (1-based), return the name of the chosen Player(name is a property of Player objects, e.g Player.name)

Example:

duck_duck_goose([a, b, c, d], 1) should return a.name
duck_duck_goose([a, b, c, d], 5) should return a.name
duck_duck_goose([a, b, c, d], 4) should return d.name
// PHP only
duck_duck_goose([$a, $b, $c, $d], 1); // => $a["name"]
duck_duck_goose([$a, $b, $c, $d], 5); // => $a["name"]
duck_duck_goose([$a, $b, $c, $d], 4); // => $d["name"]

The solution in Java code

Option 1 (returning the modulo):

public class Solution {
  public static String duckDuckGoose(Player[] players, int goose) {
    return players[(goose - 1) % players.length].name;
  }
}

Option 2 (slightly longer):

public class Solution {
  public static String duckDuckGoose(Player[] players, int goose) {
    int a = players.length;
    int b = goose%a-1;
    if(b<0) b = b+a;
    return players[b].name;
  }
}

Option 3 (working through the problem):

public class Solution {
  public static String duckDuckGoose(Player[] players, int goose) {
    int len = players.length;
    int diff = 0;
    if(goose > 0) {
      if(goose % len == 0){
        return players[len-1].name;
      }
      if(goose > len){
        diff = (goose % len) - 1;
      } else if(goose == 1) {
        diff = 0;
      }
      if(goose < len){
        diff = goose - 1;
      }
      if(goose == len){
        return players[len-1].name;
      }     
    }
    
    return players.name;
  }
}

Option 4 (using a while):

public class Solution {
  public static String duckDuckGoose(Player[] players, int goose) {
    int size = players.length ;
    while (goose > size) {
      goose = goose - size;
    } 
    return players[goose-1].name;
  }
}

Test cases to validate our Java solution

import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;

public class ExampleSolutionTests {
    @Test
    public void tests() {
      Player[] players = makePlayerArr(new String[] {"a", "b", "c", "d", "c", "e", "f", "g", "h", "z"});
      assertEquals("a", Solution.duckDuckGoose(players, 1));
      assertEquals("c", Solution.duckDuckGoose(players, 3));
      assertEquals("z", Solution.duckDuckGoose(players, 10));
      assertEquals("z", Solution.duckDuckGoose(players, 20));
      assertEquals("z", Solution.duckDuckGoose(players, 30));
      assertEquals("g", Solution.duckDuckGoose(players, 18));
      assertEquals("g", Solution.duckDuckGoose(players, 28));
      assertEquals("b", Solution.duckDuckGoose(players, 12));
      assertEquals("b", Solution.duckDuckGoose(players, 2));
      assertEquals("f", Solution.duckDuckGoose(players, 7));
    }
    
    private Player[] makePlayerArr(String[] names) {
      Player[] players = new Player[names.length];
      for (int i = 0; i < names.length; i++) {
        players[i] = new Player(names[i]);
      }
      return players;
    }
}
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments