Start learning free

LESSON 185 OF 285 · LOOPING OVER ARRAY INDICES

Employee of the Year V4 in Go: Any Number of Employees

Practice Read the full explanation

A whole team is competing. Use a for loop over the slices to find the most productive employee, then print their 15% bonus.

You start with:

names := []string{"John Doe", "Mary Jane", "Bob Smith", "Alice Chen", "Carlos Diaz"}
productivities := []int{83, 133, 97, 150, 120}
salaries := []int{135000, 335000, 210000, 250000, 180000}

Build:

Alice Chen
Bonus: $37500

Rules:

  • Index i in all three slices refers to the same employee.
  • The winner is whichever employee has the highest productivity. If two employees share the highest productivity, the one listed first wins.
  • The winner's bonus is 15% of their salary, rounded down to a whole number.
  • Print the winner's name on the first line, then "Bonus: $<amount>" on the second.

Start with bestIndex := 0 and loop over the other indices with for i := 1; i < len(names); i++. Inside the loop, update the best index only when productivities[i] > productivities[bestIndex]: a strict comparison keeps the first employee on a tie.

Dividing two whole numbers (int) drops the fraction, so salaries[bestIndex] * 15 / 100 is already rounded down: bonus := salaries[bestIndex] * 15 / 100

Join text and a number with strconv.Itoa: fmt.Println("Bonus: $" + strconv.Itoa(bonus))

names := []string{"John Doe", "Mary Jane", "Bob Smith", "Alice Chen", "Carlos Diaz"}
productivities := []int{83, 133, 97, 150, 120}
salaries := []int{135000, 335000, 210000, 250000, 180000}

bestIndex := 0
for i := 1; i < len(names); i++ {
    if productivities[i] > productivities[bestIndex] {
        bestIndex = i
    }
}
bonus := salaries[bestIndex] * 15 / 100
fmt.Println(names[bestIndex])
fmt.Println("Bonus: $" + strconv.Itoa(bonus))

STEP 1 OF 6
    Ctrl + Enter to check

    GO · LOOPING OVER ARRAY INDICES

    Employee Of The Year V4 in Go, explained

    Problem overview

    Version 4 stores the team in slices, so it can be any size. Three slices describe the employees, and index i in each of them refers to the same person:

    names := []string{"John Doe", "Mary Jane", "Bob Smith", "Alice Chen", "Carlos Diaz"}
    productivities := []int{83, 133, 97, 150, 120}
    salaries := []int{135000, 335000, 210000, 250000, 180000}
    

    The most productive employee wins, and on a tie the one listed first wins. The program prints the winner's name and a bonus of 15% of their salary, rounded down. Alice Chen has the highest productivity, 150, and 250000 * 15 / 100 is 37500:

    Alice Chen
    Bonus: $37500
    

    Approach: a loop over positions that tracks the best index

    A chain of if statements would need a longer condition with every new employee. A loop works for any length. It keeps bestIndex, the position of the most productive employee seen so far, and updates it whenever someone beats them.

    Because the three slices line up, one number is enough to read the winner's name, productivity and salary: names[bestIndex], productivities[bestIndex] and salaries[bestIndex]. Tracking the position instead of copying values into separate variables keeps the code short.

    On the starting data, bestIndex starts at 0 (83). Position 1 has 133, so it becomes 1. Position 2 (97) and position 3 (150): 150 is greater than 133, so it becomes 3. Position 4 (120) is lower. The loop ends with bestIndex equal to 3, which is Alice Chen.

    Step by step

    1. Start bestIndex at 0. Until someone beats employee 0, they are the best known.
    2. Loop from 1: for i := 1; i < len(names); i++ {. Employee 0 is already the starting best, so there is nothing to compare at position 0.
    3. Compare strictly. Inside the loop, if productivities[i] > productivities[bestIndex], set bestIndex = i. The strict comparison leaves the earlier employee ahead on a tie.
    4. Compute the bonus after the loop as salaries[bestIndex] * 15 / 100.
    5. Print the name, names[bestIndex].
    6. Print the bonus line, converting the number with strconv.Itoa.

    The solution in Go

    names := []string{"John Doe", "Mary Jane", "Bob Smith", "Alice Chen", "Carlos Diaz"}
    productivities := []int{83, 133, 97, 150, 120}
    salaries := []int{135000, 335000, 210000, 250000, 180000}
    
    bestIndex := 0
    for i := 1; i < len(names); i++ {
        if productivities[i] > productivities[bestIndex] {
            bestIndex = i
        }
    }
    bonus := salaries[bestIndex] * 15 / 100
    fmt.Println(names[bestIndex])
    fmt.Println("Bonus: $" + strconv.Itoa(bonus))
    

    The code uses the fmt and strconv packages.

    The details worth knowing

    • Slice literals. []string{...} creates a slice, a growable view over an array. The element type comes first and the values go in braces.
    • The three-part for. Go's only loop keyword covers counting loops. i++ is a statement, not an expression, so it cannot be used inside a larger expression.
    • Index out of range panics. A valid index runs from 0 to len(names) - 1. Using i <= len(names) as the loop condition would read position 5 of a five-element slice and stop the program with a panic.
    • Integer arithmetic. Multiplying before dividing by 100 keeps the rounding behaviour the lesson expects: the division drops the fraction.

    Complexity

    Time is O(n) for n employees: one pass with constant work per employee. Extra space is O(1), since the slices already exist and only bestIndex, i and bonus are added.

    Edge cases and common mistakes

    • Ties. With productivities {150, 150, 97}, the strict > leaves bestIndex at 0. Writing >= moves it to 1 and picks the later employee.
    • An empty team. If the slices are empty, productivities[bestIndex] and salaries[bestIndex] read position 0 of an empty slice and panic with index out of range [0] with length 0. The lesson's data always has employees.
    • A single employee. The loop body never runs, bestIndex stays 0 and the lone employee wins.
    • Slices of different lengths. The loop uses len(names), so a shorter productivities would panic. Keep the three slices aligned.
    • Remembering the value instead of the position. If you only store the best productivity, you can no longer find the name and the salary that belong to it.

    Testing

    • The starting data prints Alice Chen and Bonus: $37500.
    • Productivities {150, 150, 97, 100, 120} print John Doe and Bonus: $20250.
    • Productivities {10, 20, 30, 40, 50} print Carlos Diaz and Bonus: $27000, the last position.
    Keep going with Go1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free