Problem overview
Version 4 removes the limit on the number of employees. The data is now three parallel arrays, names, productivities and salaries, and index i in all three describes the same person.
The rules are unchanged. The winner has the highest productivity, a tie goes to the employee listed first, and the bonus is 15% of the winner's salary, rounded down. For the five employees in the starter code, the highest productivity is 150, which belongs to Alice Chen at index 3. Her salary is 250000, so the output is:
Alice Chen
Bonus: $37500
Approach
Instead of copying a name and a salary around, the program remembers only a position: bestIndex, the index of the best employee seen so far. One index gives access to the name, the productivity and the salary, because the arrays line up.
The idea is a running maximum. Assume employee 0 is the best, then visit each other employee in turn. When someone has strictly higher productivity than the current best, the best index moves to them. After the last employee, bestIndex points at the winner. The loop works for any number of employees, from two to thousands, with the same code.
Tracing the example, bestIndex starts at 0 (productivity 83). Index 1 has 133, which is higher, so bestIndex becomes 1. Index 2 has 97, no change. Index 3 has 150, so bestIndex becomes 3. Index 4 has 120, no change. The loop ends with 3.
Step by step
- Create
bestIndexat 0. It is aletof typenumber, because it changes as better employees appear. Starting at 0 means the first employee is the best until someone beats them. - Write the loop from 1. A
forloop withistarting at 1 and running whilei < names.lengthvisits every other employee. It starts at 1 because employee 0 is already the best so far, and comparing them with themselves would do nothing. - Compare inside the loop. If
productivities[i]is strictly greater thanproductivities[bestIndex], setbestIndextoi. Strictly greater matters: on a tie, the employee who is already best, and who is listed earlier, keeps the lead. - Compute the bonus after the loop, from
salaries[bestIndex], asMath.floor(salaries[bestIndex] * 15 / 100). - Print the name with
names[bestIndex]. - Print the bonus line by joining
"Bonus: $"andbonuswith+.
The solution in TypeScript
const names: string[] = ["John Doe", "Mary Jane", "Bob Smith", "Alice Chen", "Carlos Diaz"];
const productivities: number[] = [83, 133, 97, 150, 120];
const salaries: number[] = [135000, 335000, 210000, 250000, 180000];
let bestIndex: number = 0;
for (let i: number = 1; i < names.length; i++) {
if (productivities[i] > productivities[bestIndex]) {
bestIndex = i;
}
}
const bonus: number = Math.floor(salaries[bestIndex] * 15 / 100);
console.log(names[bestIndex]);
console.log("Bonus: $" + bonus);
TypeScript details to keep in mind:
- Array types.
string[]is an array of strings andnumber[]an array of numbers. The annotations are erased when the code is compiled, so at run time these are ordinary JavaScript arrays. - Loop variable.
let iin theforheader is scoped to the loop, and each pass has its own binding.i++adds one after every pass. - Numbers can be fractions. The division by 100 may leave decimals, so
Math.flooris still needed.
Complexity
Time: O(n). The loop visits each employee once, doing a constant amount of work. The bonus and the printing are constant.
Space: O(1). The program stores one index and one bonus, regardless of how many employees there are.
Edge cases and common mistakes
- Ties. If Alice Chen and Carlos Diaz both have 150, Alice wins, because Carlos's 150 isn't strictly greater than hers. Using
>=hands the win to the last employee with the top value: with Carlos Diaz's productivity changed to 150, the output becomesCarlos DiazandBonus: $27000. - Starting the loop at 0. It gives the same answer, because comparing employee 0 with themselves changes nothing, but it does one wasted pass. The lesson starts at 1 to make the idea clear.
- Keeping the best productivity instead of the best index. A running maximum of the value tells you how high it is, but not who has it. The index gives the name and the salary too.
- Mixing up the arrays. The comparison uses
productivities; the bonus usessalaries; the name comes fromnames. All three sharebestIndex. - The winner is the first employee. If employee 0 is the most productive, the loop never changes
bestIndex, and the output is correct.
Testing
- The starter data prints
Alice ChenandBonus: $37500. - Productivities
[150, 120, 150, 10, 5]printJohn DoeandBonus: $20250: the later 150 doesn't replace the first. - Productivities
[10, 20, 30, 5, 5]printBob SmithandBonus: $31500. - Productivities
[83, 133, 97, 150, 150]printAlice ChenandBonus: $37500.
