Problem overview
The method oldestPet gets a list of Pet records, each with a name and an age, and returns the name of the oldest one. If several pets share the top age, the one that comes first in the list wins. The lesson provides the record: record Pet(String name, int age) {}.
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.
Approach
Keep the oldest pet found so far and compare each pet with it. Store the whole Pet, not just the age, so its name comes along. One pass is enough.
Step by step
- Start
oldestas the first pet,pets.get(0). Before the loop looks at anything else, it is the oldest you have seen. - Loop over the list with
for (Pet pet : pets). - Inside, test
pet.age() > oldest.age(). The comparison is strict. A pet of the same age must not replace the one you have, which keeps the earlier one on a tie. - When the test passes, set
oldest = pet. - After the loop, return
oldest.name().
Following the tie
Take Luna 3, Coco 9, Milo 9. This shows the name held by oldest after the loop looks at each pet:
| pet | oldest after the test |
|---|---|
| Luna | Luna |
| Coco | Coco |
| Milo | Coco |
Coco is older than Luna, so she takes over. Milo is exactly as old as Coco, which is not older, so Coco stays. The strict test is what turns "first in the list" into code, with no extra rule.
Pets are records, so oldest = pet copies a reference to a record that never changes. Nothing can alter the pet you are holding by accident.
The solution in Java
class Solution {
public String oldestPet(List<Pet> pets) {
Pet oldest = pets.get(0);
for (Pet pet : pets) {
if (pet.age() > oldest.age()) {
oldest = pet;
}
}
return oldest.name();
}
}
Reading a record's values takes parentheses: pet.age() and oldest.name(). The loop starts with the first pet again, compares it with itself and changes nothing, which is harmless.
Edge cases and common mistakes
- Using
>=. OnLuna 3, Coco 9, Milo 9it returnsMilo, because the later pet replaces the earlier one on a tie. The expected answer isCoco. - Keeping only the age. A variable holding just the oldest age can't give you the name at the end. Keep the whole pet.
- One pet.
Kiki 0returnsKiki. Starting from the first pet, instead of from a made-up age of 0, makes this case work without special handling.
Testing
Check your method on these lists. The expected results come from the lesson's examples:
Rex 4, Bella 7, Max 2returnsBella.Luna 3, Coco 9, Milo 9returnsCoco.Kiki 0returnsKiki.
Also try a list where the oldest pet is first, and one where it is last. A loop that works when the answer is in the middle but not at an end usually has a wrong starting value or a wrong loop range. Here neither can go wrong, because the loop covers every pet and starts from a real one.
The method visits each pet once and keeps a single extra variable, so its time grows in step with the number of pets and it needs no extra memory beyond that variable. Sorting the pets by age would also find the oldest, but it does more work and has to deal with ties explicitly.
