Problem overview
Three parallel arrays describe the brands: ratings[i] and reviews[i] belong to brands[i]. The program prints the top k brands as rank. Brand (rating, reviews). The rating decides first. When two brands have the same rating, the one with more reviews wins. If both are equal, the brand listed first wins. This version builds on the repeated search of version 4, with no filtering; only the comparison gets a second criterion.
The starter code:
| index | brand | rating | reviews |
|---|---|---|---|
| 0 | TechPro | 4 | 200 |
| 1 | MegaCorp | 5 | 150 |
| 2 | NeoByte | 5 | 500 |
| 3 | Quantum | 3 | 100 |
| 4 | Zenith | 4 | 300 |
With k = 3 the output is:
1. NeoByte (5, 500)
2. MegaCorp (5, 150)
3. Zenith (4, 300)
NeoByte and MegaCorp share the top rating, and NeoByte has more reviews, so it comes first. Zenith and TechPro share rating 4, and Zenith has more reviews.
Approach
The search is the same as before. Each round scans all brands, skips the ones already taken (flagged in a used array), and keeps the position of the best in bestIndex. The winner is flagged and a line is added.
The only change is the test that decides whether brand i beats the current best. It becomes a chain of three cases, checked in order:
- Nothing is found yet (
bestIndex == -1): this brand is the best so far. - This rating is higher than the best one's: it becomes the best, and reviews don't matter.
- The ratings are equal and this brand has more reviews: it becomes the best.
Because the chain uses else if, a case runs only when the ones above it were false. When the ratings and the reviews are both equal, no case matches, the current best stays, and the brand listed first wins.
On the lesson's data, round 1 ends with index 2 (NeoByte: rating 5, 500 reviews beats MegaCorp's 150). Round 2 picks index 1 (MegaCorp), the only brand left with rating 5. Round 3 picks index 4 (Zenith) over index 0 (TechPro), because both have rating 4 and 300 reviews is more than 200.
Step by step
- Create
usedwith one slot per rating, allfalse. - Create
lines, an emptymutableListOf<String>(). - Loop over the ranks with
for (rank in 1..k). The counter is also the number printed before each brand. - Reset
bestIndexto -1 in every round. - Write the inner loop over the positions of
ratings. - Skip taken brands with
if (!used[i]). - First case: when
bestIndexis -1, set it toi. - Second case, with
else if: the rating is higher than the best one's. - Third case, with another
else if: the ratings are equal and the reviews are higher. Both conditions must hold. - Mark the winner after the inner loop:
used[bestIndex] = true. - Add the line: the rank, a dot and a space, the name, then the rating and the review count in parentheses, separated by a comma and a space.
- Print the lines joined with newlines.
The solution in Kotlin
val brands = arrayOf("TechPro", "MegaCorp", "NeoByte", "Quantum", "Zenith")
val ratings = intArrayOf(4, 5, 5, 3, 4)
val reviews = intArrayOf(200, 150, 500, 100, 300)
val k = 3
val used = BooleanArray(ratings.size)
val lines = mutableListOf<String>()
for (rank in 1..k) {
var bestIndex = -1
for (i in ratings.indices) {
if (!used[i]) {
if (bestIndex == -1) {
bestIndex = i
}
else if (ratings[i] > ratings[bestIndex]) {
bestIndex = i
}
else if (ratings[i] == ratings[bestIndex] && reviews[i] > reviews[bestIndex]) {
bestIndex = i
}
}
}
used[bestIndex] = true
lines.add("$rank. ${brands[bestIndex]} (${ratings[bestIndex]}, ${reviews[bestIndex]})")
}
println(lines.joinToString("\n"))
Kotlin details worth a look:
elseon a new line. Theelse ifkeywords sit on their own lines after the closing brace. Kotlin accepts that inside a statement chain; the lesson's formatting is only a style choice.&&needs both sides. The third case is true only when the ratings match and the reviews are higher. A lower rating with many reviews never reaches it, because the second case or the final fall-through has already decided.- Templates with expressions. In the line,
$rankneeds no braces, while${brands[bestIndex]}and the other array reads do. The comma and the space between the two numbers are plain text in the string. - Three arrays, one index.
bestIndexis enough to look up the name, the rating and the reviews, because all three arrays line up.
Complexity
Time: O(k * n) for n brands, since each of the k rounds scans all n and each comparison costs a constant amount. The extra criterion doesn't change the order of growth. Space: O(n) for used, plus O(k) for the lines.
Edge cases and common mistakes
- Ranking by reviews alone. Comparing only
reviewswould give NeoByte first here, which happens to be right, but a brand with rating 3 and 9,000 reviews would beat one with rating 5 and 10. With names Loud (3, 9000) and Good (5, 10), the output is1. Good (5, 10)then2. Loud (3, 9000), because the rating decides first. - Forgetting the equal-rating condition. In the third case,
reviews[i] > reviews[bestIndex]alone, withoutratings[i] == ratings[bestIndex], would let a lower-rated brand with many reviews replace a better one. - A tie on rating where the later brand has more reviews. A (5, 10) and B (5, 20) print as
1. B (5, 20)and2. A (5, 10). - A tie on everything. A, B and C, each rated 4 with 10 reviews, print in listing order: A, B, C. That needs strict
>in both comparisons. With>=on the reviews, a later brand would replace an earlier one that is equal in both. - Missing
usedflags. Every round would pick NeoByte again. klarger than the number of brands. The task rules it out. Then a round finds nobody andused[-1]throws. Withk = 5every brand is listed: NeoByte, MegaCorp, Zenith, TechPro, Quantum.- Resetting
bestIndextoo early or too late. It belongs at the start of each round, inside the outer loop.
Testing
| data | output |
|---|---|
the lesson's five brands, k = 3 | 1. NeoByte (5, 500), 2. MegaCorp (5, 150), 3. Zenith (4, 300) |
the same, k = 1 | 1. NeoByte (5, 500) |
the same, k = 5 | NeoByte, MegaCorp, Zenith (4, 300), TechPro (4, 200), Quantum (3, 100) |
Loud (3, 9000) and Good (5, 10), k = 2 | 1. Good (5, 10), 2. Loud (3, 9000) |
A (5, 10) and B (5, 20), k = 2 | 1. B (5, 20), 2. A (5, 10) |
A, B, C all (4, 10), k = 3 | 1. A (4, 10), 2. B (4, 10), 3. C (4, 10) |
