Interview Question
Country: United States
1.- I have created two classes, one for receiving the scores after every game. the second contains the main class and the process flow for starting the tournament and scoring after every game.
{}
package premierleagueplayers;
/**
*
* @author hrmnn
*/
public class PlayerScore{
int idPlayer;
int score;
public PlayerScore(int idPlayer, int score ){
this.idPlayer=idPlayer;
this.score=score;
}
}
package premierleagueplayers;
import java.util.*;
import javafx.util.Pair;
/**
*
* @author hrmnn
*/
public class PremierLeaguePlayers {
int numberOfPlayers;
int numberOfGames;
Map<Integer,Integer> scores;
public PremierLeaguePlayers(){
this.scores=new HashMap<Integer, Integer>();
}
/**
*
*/
public void printScore(){
System.out.println("Printing Score");
for(Integer id:this.scores.keySet()){
int key=id.intValue();
int value=this.scores.get(id).intValue();
System.out.println(key + "=" +value);
}
}
public void startTournament(int players, int games){
this.numberOfGames=games;
this.numberOfPlayers=players;
System.out.println("starting tournament");
for(int i=1;i<=this.numberOfPlayers;i++){
this.scores.put(i, 0);
}
}
private void updatingScores(PlayerScore[] args){
int playerValue;
for(int i=0;i<args.length;i++){
playerValue=this.scores.get(args[i].idPlayer);
this.scores.replace(args[i].idPlayer, args[i].score+playerValue);
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
PremierLeaguePlayers rankings= new PremierLeaguePlayers();
rankings.startTournament(10,10);
rankings.printScore();
System.out.println("Scores after first game");
PlayerScore[] score1= new PlayerScore[10];
score1[0]=new PlayerScore(1,3);
score1[1]=new PlayerScore(2,4);
score1[2]=new PlayerScore(3,10);
score1[3]=new PlayerScore(4,1);
score1[4]=new PlayerScore(5,0);
score1[5]=new PlayerScore(6,5);
score1[6]=new PlayerScore(7,2);
score1[7]=new PlayerScore(8,7);
score1[8]=new PlayerScore(9,2);
score1[9]=new PlayerScore(10,1);
rankings.updatingScores( score1);
rankings.printScore();
System.out.println("Scores after second game");
score1[0]=new PlayerScore(1,3);
score1[1]=new PlayerScore(2,4);
score1[2]=new PlayerScore(3,3);
score1[3]=new PlayerScore(4,1);
score1[4]=new PlayerScore(5,4);
score1[5]=new PlayerScore(6,5);
score1[6]=new PlayerScore(7,2);
score1[7]=new PlayerScore(8,7);
score1[8]=new PlayerScore(9,2);
score1[9]=new PlayerScore(10,10);
rankings.updatingScores( score1);
rankings.printScore();
}
}
1. Use a Map<Int, Int> for keeping scores of each player by ID, Points.
2. after every game build a rank by transforming the list to a sequence and sorting it according to points.In Scala this could be done in a functional way like this:
- guilhebl April 19, 2018