Interview Question


Country: United States




Comment hidden because of low score. Click to expand.
0
of 0 vote

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:

// players is a Map[Int, Int]
val rankings = players.toSeq.sortBy(_._2):_*

- guilhebl April 19, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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();


}


}

- hrmnn2005 April 21, 2018 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More