Start learning free

LESSON 99 OF 285 · CONDITIONAL STATEMENTS - CHALLENGES

Top K Brands V2 in Go: The Better of Two Brands

Practice Read the full explanation

Two brands sell the same product. Show the one with the higher rating.

You start with:

brand1 := "TechPro"
rating1 := 4
brand2 := "MegaCorp"
rating2 := 5

var bestBrand string
var bestRating int

Build:

1. MegaCorp (5)

Rules:

  • Use if/else to pick the winner: brand1 when rating1 > rating2, otherwise brand2.
  • The starter already declares bestBrand and bestRating: assign the winner's name and rating to them inside the branches.
  • Format the result as "1. Brand (rating)" and print it with fmt.Println(...).

Pick the winner into bestBrand and bestRating first, then build the output text from them.

Go needs strconv.Itoa(...) to turn an int into text before joining it with +.

brand1 := "TechPro"
rating1 := 4
brand2 := "MegaCorp"
rating2 := 5

var bestBrand string
var bestRating int
if rating1 > rating2 {
    bestBrand = brand1
    bestRating = rating1
} else {
    bestBrand = brand2
    bestRating = rating2
}
fmt.Println("1. " + bestBrand + " (" + strconv.Itoa(bestRating) + ")")

STEP 1 OF 5
    Ctrl + Enter to check

    GO · CONDITIONAL STATEMENTS - CHALLENGES

    Top K Brands V2 in Go, explained

    Problem overview

    This version has two brands selling the same product: brand1 with rating1, and brand2 with rating2. The program prints the one with the higher rating as a ranked entry, 1. Brand (rating). With the starting data, TechPro has 4 and MegaCorp has 5, so the output is:

    1. MegaCorp (5)
    

    The starter already declares two empty variables, bestBrand and bestRating. The lesson fills them in from the winner's data, then prints once.

    Approach: choose, then format once

    The winner is picked by comparing the two ratings. The formatting of the entry is the same whichever brand wins, so it should not be written twice. The pattern is to let the if/else copy the winner's name and rating into bestBrand and bestRating, and to build the output line after the branches from those two variables.

    The rule is that brand1 wins only when rating1 > rating2. On a tie, the else branch runs and brand2 is chosen. This is a strict comparison, and the tie behaviour is part of the lesson's contract.

    Traced on the starting data: 4 > 5 is false, so the else branch runs. bestBrand becomes "MegaCorp" and bestRating becomes 5, and the last line prints 1. MegaCorp (5). If the ratings were swapped, the if branch would run and TechPro would be printed with its own rating.

    Why keep both the name and the rating, when the rating alone decides the winner? Because the output needs both, and once the branches have run, the program no longer knows which brand won unless the variables remember it. Storing the pair is the simplest answer for two brands.

    Before the if, bestBrand and bestRating hold the zero values. Printing them at that point would give an empty brand and a rating of 0, which is a quick way to see that the branches really do the filling.

    Because the starter already declares both variables, the lesson's own code only assigns to them. That is the reason the branches use = and not :=. The operator := would declare a second, new variable inside the branch, and the outer one would never change. When you write this pattern yourself in another program, the declaration has to come before the if, in the scope that the later code can see.

    Step by step

    1. Write the if. The condition is rating1 > rating2. Inside it, assign brand1 to bestBrand.
    2. Assign the rating in the same branch: bestRating = rating1. The brand and its rating always travel together.
    3. Add the else, on the same line as the closing brace of the if, and assign brand2 to bestBrand in it.
    4. Assign the second rating: bestRating = rating2 inside the else, then close the brace.
    5. Print the entry. Join "1. ", bestBrand, " (", strconv.Itoa(bestRating) and ")" with + and pass the string to fmt.Println.

    The solution in Go

    brand1 := "TechPro"
    rating1 := 4
    brand2 := "MegaCorp"
    rating2 := 5
    
    var bestBrand string
    var bestRating int
    if rating1 > rating2 {
        bestBrand = brand1
        bestRating = rating1
    } else {
        bestBrand = brand2
        bestRating = rating2
    }
    fmt.Println("1. " + bestBrand + " (" + strconv.Itoa(bestRating) + ")")
    

    The program uses the fmt and strconv packages.

    The details worth knowing

    • Declared before the branches. var bestBrand string creates a variable holding "", and var bestRating int creates one holding 0. Both live in the surrounding scope, so the Println after the if can read them. If a branch wrote bestBrand := brand1 instead of bestBrand = brand1, it would create a new variable that disappears at the closing brace. The compiler would report declared and not used for it, and the outer bestBrand would stay empty.
    • The condition must be a bool. rating1 > rating2 is one. There are no parentheses around it, and the braces are required.
    • Assigning is not using. Go reports a variable as unused when it is only ever assigned and never read. If the final line printed only bestBrand, the build would stop with bestRating declared and not used, even though both branches assign it. The final Println reads both variables, so the program compiles.
    • Text and numbers. The rating is an int, so it goes through strconv.Itoa before + can join it to strings.

    Complexity

    There is a single comparison and a few assignments, so time and extra space are O(1).

    Edge cases and common mistakes

    • A tie. With rating1 := 5 and rating2 := 5 the condition is false and the output names brand2. Using >= would give the tie to brand1 and fail the lesson's tests.
    • Copying only the name. If a branch sets bestBrand but forgets bestRating, the printed rating is 0, or stays at whatever value an earlier assignment left.
    • Printing the wrong rating. Printing rating1 in the last line, instead of bestRating, shows the first brand's rating next to the second brand's name whenever the second brand wins.
    • Reversed comparison. rating1 < rating2 as the condition would reverse the choice, so the lower-rated brand would be printed.
    • Formatting inside both branches. It works, but the entry format is then written twice. Keep it after the if/else.

    Testing

    • TechPro 4 and MegaCorp 5 print 1. MegaCorp (5).
    • TechPro 5 and MegaCorp 4 print 1. TechPro (5).
    • TechPro 3 and MegaCorp 3 print 1. MegaCorp (3), the second brand on a tie.
    Keep going with Go1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free