Interview Question – 9

By | October 13, 2018

Question

Maria plays college basketball and wants to go pro. Each season she maintains a record of her play. She tabulates the number of times she breaks her season record for most points and least points in a game. Points scored in the first game establish her record for the season, and she begins counting from there.

For example, assume her scores for the season are represented in the array scores=[12,24,10,24]. Scores are in the same order as the games played. She would tabulate her results as follows:

Game Score Minimum Maximum Min Max
0 12 12 12 0 0
1 24 12 24 0 1
2 10 10 24 1 1
3 24 10 24 1 1

Given Maria’s scores for a season, find and print the number of times she breaks her records for most and least points scored during the season.

You can download the problem statement from here.

Sample Input

10
3,4, 21, 36, 10, 28, 35, 5, 24, 42

Sample Output

4, 0

Solution

static int[] breakingRecords(int[] scores) {

    int min = scores[0], max = scores[0];
    int minCount = 0, maxCount = 0;

    int len = scores.length;

    for (int i = 0; i < len; i++) {
        if (scores[i] < min) {
            min = scores[i];
            minCount++;
        }
        if (scores[i] > max) {
            max = scores[i];
            maxCount++;
        }
    }
    return new int[]{maxCount, minCount};
}

Leave a Reply

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