Problem overview
The method oldestPet gets an array pets. Each pet is a tuple [string, number]: its name and its age. The method returns the name of the oldest pet. If several pets share the oldest age, the one that comes first in pets wins. The array has at least one pet.
For [["Rex", 4], ["Bella", 7], ["Max", 2]] the answer is "Bella". For [["Luna", 3], ["Coco", 9], ["Milo", 9]] it is "Coco", because Coco and Milo are both 9 and Coco comes first. For [["Kiki", 0]] it is "Kiki".
Approach
Remember the oldest pet seen so far and compare each pet with it. Start with the first pet, because before the loop looks at anything else it is the oldest you have seen. When a pet is older, it replaces the remembered one. The remembered value is the whole pair, so the name comes along with the age, and the name is read only at the end.
Why the tie goes to the first pet
The comparison is strict: >, not >=. A pet of the same age doesn't replace the one you have, so on a tie the earlier pet stays. With >=, Milo would take Coco's place in the second example.
Step by step
- Declare
oldestas a[string, number]withlet, set topets[0]. - Loop over
petswithfor...of, naming each pairpet. - In the loop, check whether
pet[1](this pet's age) is greater thanoldest[1]. - If it is, set
oldest = pet. - After the loop, return
oldest[0], the name.
The solution in TypeScript
class Solution {
oldestPet(pets: [string, number][]): string {
let oldest: [string, number] = pets[0];
for (let pet of pets) {
if (pet[1] > oldest[1]) {
oldest = pet;
}
}
return oldest[0];
}
}
The parameter type [string, number][] is an array of tuples. Index 0 of a pair is the name and index 1 is the age, so pet[1] > oldest[1] compares ages. Assigning oldest = pet doesn't copy the pair; both names point at the same array, which is fine because the code never changes it.
The first pass of the loop compares pets[0] with itself. The strict comparison is false, so nothing changes.
Tracing the second example
Take [["Luna", 3], ["Coco", 9], ["Milo", 9]]. oldest starts as Luna's pair. Coco is 9 and 9 > 3, so Coco's pair replaces it. Milo is 9, but 9 > 9 is false, so Coco stays. The method returns "Coco".
Nothing is sorted or copied: the loop visits every pet once, so the time grows in step with the number of pets, and only one extra variable is kept.
Testing
| pets | Result |
|---|---|
[["Rex", 4], ["Bella", 7], ["Max", 2]] | Bella |
[["Luna", 3], ["Coco", 9], ["Milo", 9]] | Coco |
[["Kiki", 0]] | Kiki |
These are the results of calling the method on the statement's examples.
Common mistakes
- Using
>=. The last of the oldest pets wins instead of the first. - Starting
oldestat an age of 0. Ages start at 0, so a lone pet of age 0 (Kiki) would have no name to return. Start frompets[0]. - Mixing up the indexes.
pet[0]is the name, a string; comparing names with>compiles but sorts alphabetically instead of by age. - Sorting first. Sorting the array by age would also work, but it changes the caller's array and needs a tie rule; one pass with a strict comparison is simpler.
- Returning the pair. The method returns a
string, so returnoldest[0], notoldest.
