Start learning free

LESSON 252 OF 667 · NESTED LOOPS

Top K Brands V6 in C++: Tie-Break by Review Count

Practice Read the full explanation

Rank the top k brands by rating, breaking ties with the review count. This builds on v4's repeated search (not on v5: there is no filtering here); only the comparison gets a second criterion.

You start with:

vector<string> brands = {"TechPro", "MegaCorp", "NeoByte", "Quantum", "Zenith"};
vector<int> ratings = {4, 5, 5, 3, 4};
vector<int> reviews = {200, 150, 500, 100, 300};
int k = 3;

Build:

1. NeoByte (5, 500)
2. MegaCorp (5, 150)
3. Zenith (4, 300)

Rules:

  • Rating decides first: the higher rating wins.
  • If two brands have the same rating, the one with more reviews wins.
  • If rating and reviews are both equal, the one listed first wins (the test data has no such pair, but your code must follow the rule).
  • k is never larger than the number of brands.
  • Each line is "rank. Brand (rating, reviews)", starting at rank 1.
  • Print the lines, one per line, with cout << ... << endl.

Start from v4: the same vector<bool> used(ratings.size(), false); and vector<string> lines;, the same two loops. Only the comparison inside the inner loop changes.

Use a chain (if / else if / else if): no candidate yet, take this one; else a higher rating, take this one; else the same rating and more reviews, take this one.

vector<string> brands = {"TechPro", "MegaCorp", "NeoByte", "Quantum", "Zenith"};
vector<int> ratings = {4, 5, 5, 3, 4};
vector<int> reviews = {200, 150, 500, 100, 300};
int k = 3;

vector<bool> used(ratings.size(), false);
vector<string> lines;
for (int rank = 1; rank <= k; rank++) {
    int bestIndex = -1;
    for (int i = 0; i < (int)ratings.size(); i++) {
        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.push_back(to_string(rank) + ". " + brands[bestIndex] + " (" + to_string(ratings[bestIndex]) + ", " + to_string(reviews[bestIndex]) + ")");
}
for (int i = 0; i < (int)lines.size(); i++) cout << lines[i] << endl;

STEP 1 OF 12
    Ctrl + Enter to check

    C++ · NESTED LOOPS

    Top K Brands V6 in C++, explained

    Problem overview

    Version 6 ranks the top k brands by rating, and uses the number of reviews to break ties. A third vector, reviews, sits next to brands and ratings. The rules are: a higher rating wins; if the ratings are equal, more reviews win; if both are equal, the brand listed first wins. Each line is rank. Brand (rating, reviews), and k is never larger than the number of brands.

    With k = 3 the program prints:

    1. NeoByte (5, 500)
    2. MegaCorp (5, 150)
    3. Zenith (4, 300)
    

    MegaCorp and NeoByte both have rating 5, and NeoByte's 500 reviews beat 150. At rating 4, Zenith's 300 reviews beat TechPro's 200. The version builds on the repeated search from the fourth version of this program: the filtering is not part of it, and only the comparison gets a second criterion.

    Approach: a comparison with two criteria

    The repeated search stays as it is: each round finds the best brand that isn't taken, marks it, and adds a line. What changes is how "better" is decided. The rule has three parts, which fit an if / else if chain, and the order of the chain matters:

    1. If no brand has been found yet, take this one.
    2. Otherwise, if its rating is higher than the best one's, take it. Reviews don't matter at all here.
    3. Otherwise, if the ratings are equal and it has more reviews, take it.

    Each case runs only when the ones above it were false. That is what lets the second case say simply "higher rating": a lower rating fails both the second and third cases, so the brand is not taken. Nothing is said about equal ratings with equal reviews, so the earlier brand stays, which is the rule for a full tie.

    The chain is also easier to read than one long condition. A single expression would have to say "no best yet, or a higher rating, or an equal rating with more reviews", with parentheses to keep the groups straight. Three short cases, each with one idea, are easier to check against the rules one by one.

    Step by step

    1. Create used with one false for each rating.
    2. Create the empty vector<string> lines.
    3. Loop rank from 1 to k inclusive. It counts the rounds and is the number printed first on each line.
    4. Start the round with bestIndex = -1.
    5. Loop i over every position of ratings.
    6. Look only at brands that aren't taken: if (!used[i]).
    7. First case: when bestIndex is -1, this brand is the best so far, so set bestIndex = i.
    8. Second case: else if the rating is higher than the best one's, it becomes the best.
    9. Third case: else if the ratings are equal and this brand has more reviews, it becomes the best.
    10. Mark the winner with used[bestIndex] = true.
    11. Add the line with push_back: rank, ". ", name, " (", rating, ", ", reviews and ")". Numbers go through to_string.
    12. Print every line in a final loop.

    The solution in C++

    vector<string> brands = {"TechPro", "MegaCorp", "NeoByte", "Quantum", "Zenith"};
    vector<int> ratings = {4, 5, 5, 3, 4};
    vector<int> reviews = {200, 150, 500, 100, 300};
    int k = 3;
    
    vector<bool> used(ratings.size(), false);
    vector<string> lines;
    for (int rank = 1; rank <= k; rank++) {
        int bestIndex = -1;
        for (int i = 0; i < (int)ratings.size(); i++) {
            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.push_back(to_string(rank) + ". " + brands[bestIndex] + " (" + to_string(ratings[bestIndex]) + ", " + to_string(reviews[bestIndex]) + ")");
    }
    for (int i = 0; i < (int)lines.size(); i++) cout << lines[i] << endl;
    

    The two conditions in the third case are joined with &&: both must hold, the same rating and more reviews. Writing ratings[i] == ratings[bestIndex] || reviews[i] > reviews[bestIndex] would let a brand with a lower rating win on reviews alone.

    The first case checks bestIndex == -1 before anything reads ratings[bestIndex]. Because the cases form a chain, the later ones are never evaluated while bestIndex is -1.

    Following the rounds

    rankwinnerwhy
    1NeoByte (5, 500)ties MegaCorp on rating, more reviews
    2MegaCorp (5, 150)the only brand with rating 5 left
    3Zenith (4, 300)ties TechPro on rating, more reviews

    Complexity

    Each of the k rounds scans all n brands, so the time is O(k × n). The extra space is O(n) for used and O(k) for lines.

    Edge cases and common mistakes

    • Reviews deciding too early. If you compare reviews first, a brand with a lower rating but many reviews would beat a better-rated one. Rating decides first.
    • A full tie. With ratings 5 and 5 and reviews 10 and 10, both comparisons are false for the later brand, so the earlier one wins and the lines come out in the listed order. The lesson's data has no such pair, but the code follows the rule. With reviews 10 and 20, the second brand comes first.
    • All five brands. With k = 5, the lines after the three above are 4. TechPro (4, 200) and 5. Quantum (3, 100).
    • Forgetting to mark the winner. The same brand would repeat on every line.
    • A k larger than the number of brands. The statement rules this out. In this version the extra round would leave bestIndex at -1 and the program would use used[-1], which is undefined behaviour.

    Testing

    The starter data prints the three lines above. Set k to 5 to see the full ranking, or change NeoByte's review count to 100. Then MegaCorp (150 reviews) takes the first line and NeoByte moves to the second.

    Keep going with C++650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free