Problem overview
The method oldestPet gets a list pets. Each pet is a tuple of a name and an age, like ("Rex", 4). It returns the name of the oldest pet. If several pets share the oldest age, the one that comes first in the list wins.
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 whole oldest pair seen so far in oldest, starting with pets[0]. Then check every pet: if it is strictly older than oldest, it takes over. Keeping the whole tuple, not just the age, means the name comes along for free.
The comparison must be strict, >. A pet of the same age doesn't replace the one you have, so on a tie the first pet stays. With >= the last tied pet would win, and Milo would be returned in the second example.
Step by step
- Set
oldest = pets[0]. A pet is a tuple, so the age is at index 1 and the name at index 0. - Loop over
petswithpet. - If
pet[1] > oldest[1], this pet is older. - Then set
oldest = pet. - After the loop, return
oldest[0], the name only.
The solution in Python
class Solution:
def oldestPet(self, pets: List[Tuple[str, int]]) -> str:
oldest = pets[0]
for pet in pets:
if pet[1] > oldest[1]:
oldest = pet
return oldest[0]
Running it on the three examples returns Bella, Coco and Kiki (a list with one pet returns that pet's name).
Tracing the tie example
For [("Luna", 3), ("Coco", 9), ("Milo", 9)], this is oldest after each pass:
| pet | oldest after the pass |
|---|---|
| ("Luna", 3) | ("Luna", 3) |
| ("Coco", 9) | ("Coco", 9) |
| ("Milo", 9) | ("Coco", 9) |
Milo is 9, but 9 is not greater than 9, so Coco stays. The loop's first pass compares the first pet with itself, which changes nothing.
Another way, with a built-in
Python's max accepts a key function: max(pets, key=lambda pet: pet[1])[0] returns Bella for the first example. When several items tie for the largest key, max returns the first of them, so on the tie example it also gives Coco. The loop does the same work by hand, and writing it once makes the "strictly greater" rule easy to see.
Edge cases
A list with one pet returns that pet's name, because the loop never finds a pet that is older. An age of 0 is a normal age here: [("Kiki", 0)] returns Kiki. If every pet has the same age, the first pet in the list is returned, which is exactly the tie rule. The statement guarantees at least one pet, so pets[0] is always safe.
Why keep the whole pair
You could track only the highest age in one variable and the matching name in another, updating both together. Keeping the tuple in one variable is simpler: one assignment, oldest = pet, moves name and age at the same time, so they can never get out of step.
Common mistakes
- Using
>=, which breaks the tie rule. - Returning
oldestinstead ofoldest[0]: the function returns a name, not a pair. - Mixing up the indexes:
pet[0]is the name,pet[1]the age. - Starting
oldestat 0 or at an empty value instead of the first pet. Ages can be 0, and the first pet is always a real candidate.
