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
- Start
bestIndexat 0. Until someone beats employee 0, they are the best known. - 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. - Compare strictly. Inside the loop, if
productivities[i] > productivities[bestIndex], setbestIndex = i. The strict comparison leaves the earlier employee ahead on a tie. - Compute the bonus after the loop as
salaries[bestIndex] * 15 / 100. - Print the name,
names[bestIndex]. - 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. Usingi <= 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>leavesbestIndexat 0. Writing>=moves it to 1 and picks the later employee. - An empty team. If the slices are empty,
productivities[bestIndex]andsalaries[bestIndex]read position 0 of an empty slice and panic withindex out of range [0] with length 0. The lesson's data always has employees. - A single employee. The loop body never runs,
bestIndexstays 0 and the lone employee wins. - Slices of different lengths. The loop uses
len(names), so a shorterproductivitieswould 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 ChenandBonus: $37500. - Productivities
{150, 150, 97, 100, 120}printJohn DoeandBonus: $20250. - Productivities
{10, 20, 30, 40, 50}printCarlos DiazandBonus: $27000, the last position.
