Problem overview
This version pays bonuses to the two most productive employees, the best first and the second best after. The data is the same five employees in three parallel arrays: names, productivities and salaries, where index i describes one person in all three. Each bonus is 15% of the salary, rounded down, and ties go to the employee listed first. The output has one line per winner, in the form name: $bonus.
Alice Chen has the highest productivity (150) and Mary Jane the next (133). Their bonuses are 250000 * 15 / 100 = 37500 and 335000 * 15 / 100 = 50250:
Alice Chen: $37500
Mary Jane: $50250
The previous version found the single best employee with a loop. This one reuses that loop and adds a second one.
Approach: find the best, then the best of the rest
The task needs no sorting. The second best employee is simply the most productive one among everybody except the best. So the program works in two passes:
- The first pass is the loop from the last version. It leaves the best employee's position in
bestIndex. - The second pass scans every employee again, skips
bestIndex, and keeps the position of the best of the rest insecondIndex.
The second pass has a small problem the first one didn't. The first pass can start with employee 0 as a candidate, but in the second pass employee 0 might be the overall winner and so not allowed. The program can't know a safe starting candidate in advance. The fix is a marker value: secondIndex starts at -1, which is not a position in any array and so means "nobody chosen yet". The first employee who isn't the winner is accepted automatically, and from then on a strictly higher productivity is needed to replace them.
Because the comparisons are strict, ties still go to the earlier employee in both passes.
Step by step
- Create
bestIndexas avarat 0, assuming employee 0 is the best so far. - Loop over
1 until names.size. Employee 0 is the starting best, so the scan starts at 1. - Update on a strictly higher productivity. If
productivities(i)beatsproductivities(bestIndex), setbestIndextoi. - Create
secondIndexas avarat -1, meaning nobody is chosen yet. - Write a second loop over
0 until names.size. This one starts at 0, since the winner can sit anywhere in the list and is skipped inside the loop instead. - Write the condition for becoming second best. The employee must not be the winner (
i != bestIndex), and either nobody is chosen yet (secondIndex == -1) or the productivity is strictly higher than that ofsecondIndex. Then setsecondIndextoi. - Compute
bestBonusfromsalaries(bestIndex). - Compute
secondBonusthe same way fromsalaries(secondIndex). - Print the best employee as the name, a colon, a space, a dollar sign and the bonus.
- Print the second best in the same form on the next line.
The solution in Scala
val names = Array("John Doe", "Mary Jane", "Bob Smith", "Alice Chen", "Carlos Diaz")
val productivities = Array(83, 133, 97, 150, 120)
val salaries = Array(135000, 335000, 210000, 250000, 180000)
var bestIndex = 0
for (i <- 1 until names.size) {
if (productivities(i) > productivities(bestIndex)) {
bestIndex = i
}
}
var secondIndex = -1
for (i <- 0 until names.size) {
if (i != bestIndex && (secondIndex == -1 || productivities(i) > productivities(secondIndex))) {
secondIndex = i
}
}
val bestBonus = salaries(bestIndex) * 15 / 100
val secondBonus = salaries(secondIndex) * 15 / 100
println(names(bestIndex) + ": $" + bestBonus)
println(names(secondIndex) + ": $" + secondBonus)
Scala details worth knowing:
- Short-circuit operators. In
secondIndex == -1 || productivities(i) > productivities(secondIndex), the right side is evaluated only when the left side is false. This matters: whensecondIndexis -1, the right side would readproductivities(-1)and throw an exception. The||test for -1 protects it. - Parentheses around the
||.&&binds tighter than||, so without the parentheses the condition would mean something different: "not the winner and nobody chosen yet", or "higher than the current second". The winner could then be picked as second. - Arrays use parentheses for reading elements, and
untilexcludes its end, so0 until names.sizevisits indexes 0 to 4. - The
Intbonus. Multiplying by 15 before dividing by 100 keeps the rounding down correct, and the values stay far below the 32-bit limit.
Following the program
The first loop ends with bestIndex equal to 3, Alice Chen. In the second loop, with the starter productivities 83, 133, 97, 150 and 120:
- Index 0 is not the winner and nobody is chosen yet, so
secondIndexbecomes 0 (83). - Index 1 has 133, higher than 83, so
secondIndexbecomes 1. - Index 2 has 97, which doesn't beat 133.
- Index 3 is the winner and is skipped.
- Index 4 has 120, which doesn't beat 133.
So Mary Jane, at index 1, is second.
Complexity
Time: O(n). There are two loops, each visiting every employee at most once, so the total is about 2n steps. Space: O(1), since the program only adds two indexes and two bonuses. Sorting would cost O(n log n) and isn't needed for the top two.
Testing
Keeping the names and salaries from the starter data and changing only the productivities:
| Productivities | Output |
|---|---|
| 83, 133, 97, 150, 120 | Alice Chen: $37500, then Mary Jane: $50250 |
| 150, 83, 97, 150, 120 | John Doe: $20250, then Alice Chen: $37500 |
| 100, 100, 100, 100, 100 | John Doe: $20250, then Mary Jane: $50250 |
| 200, 133, 97, 150, 120 | John Doe: $20250, then Alice Chen: $37500 |
With only two employees, Array("A", "B"), productivities 5 and 9 and salaries 100000 and 200000, the output is B: $30000 and then A: $15000.
Edge cases and common mistakes
- Ties for first place. In the second row, John Doe and Alice Chen both have 150. John Doe is first, because he is listed first. Alice Chen then becomes second best, since she is the best among the others. A tied employee is not lost.
- All equal. In the third row, the earliest employee wins, and the earliest of the others, Mary Jane, is second.
- Forgetting to skip the winner. Without
i != bestIndex, the second pass finds the winner again and prints the same line twice. - Starting the second loop at 1. If the winner is employee 0, that is harmless, but if the winner is somewhere else, employee 0 is never considered for second place.
- Starting
secondIndexat 0. If employee 0 is the winner, the second pass would begin with the winner as the second-place candidate and might never replace them. - Fewer than two employees. With one employee,
secondIndexstays at -1, andsalaries(secondIndex)throws anArrayIndexOutOfBoundsException. This version assumes at least two employees.
Key takeaways
- The second best is the best of everyone except the winner, so two simple scans replace a sort.
- A value such as -1 can mean "no choice yet" because it is not a valid index.
- Put the check for that marker first in an
||, so the array is never read at -1. - The next version builds on this one.
