Skip to content

Edvins Antonovs

Breaking the Records code challenge

My approach to solving HackerRank’s Breaking the Records code challenge.


Problem

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.

Example

scores = [12, 24, 10, 24]

Scores are in the same order as the games played. She tabulates her results as follows:

1Count
2 Game Score Minimum Maximum Min Max
3 0 12 12 12 0 0
4 1 24 12 24 0 1
5 2 10 10 24 1 1
6 3 24 10 24 1 1

Given the scores for a season, determine the number of times Maria breaks her records for most and least points scored during the season.

Returns

int[2]: An array with the numbers of times she broke her records. Index 0 is for breaking most points records, and index 1 is for breaking least points records.

Input format

The first line contains an integer n, the number of games.

The second line contains n space-separated integers describing the respective values of scores0, scores1,...scoresn-1.

Sample input

19
210 5 20 20 4 5 2 25 1

Sample output

12 4

Solution

Breaking the record was a relatively easy challenge to implement. Initially, we created a bunch of variables to hold max (hiScore) and min (loBroke) values and variables for the cases when the high (hiBroke) and lowest (loBroke) scores were updated. For the values which hold the actual score, we assign it to the first array's element.

Then we start the for loop and go through every array element. We compare the current value (scores[i]) twice during each loop.

The first time, we want to check if the current value is bigger than the current highest score (hiScore) value. If so, it becomes the new highest score value, and we bump hiBroke by 1 as it is a new record break.

During the second comparison, we do the same, but this time we check for the current value (scores[i]) being less than the lowest score (loScore). The same story happens here; if it's less, it means a new record break, and we update the loScore bump loBroke by 1.

1function breakingRecords(scores) {
2 let hiScore = scores[0],
3 loScore = scores[0];
4 let hiBroke = 0,
5 loBroke = 0;
6
7 for (let i = 0; i < scores.length; i++) {
8 if (scores[i] > hiScore) {
9 hiScore = scores[i];
10 hiBroke++;
11 }
12
13 if (scores[i] < loScore) {
14 loScore = scores[i];
15 loBroke++;
16 }
17 }
18
19 return [hiBroke, loBroke];
20}

Appendix

Subscribe to the newsletter

Get emails from me about web development, tech and entrepreneurship.

© 2022 by Edvins Antonovs. All rights reserved.