Method Chaining - Problem

Given a DataFrame animals with columns name, species, age, and weight, write a solution to list the names of animals that weigh strictly more than 100 kilograms.

Return the animals sorted by weight in descending order.

Input & Output

Example 1 — Basic Filtering
$ Input: animals = [{"name":"Lion","species":"Panthera leo","age":5,"weight":80},{"name":"Elephant","species":"Loxodonta africana","age":10,"weight":150}]
Output: ["Elephant"]
💡 Note: Only Elephant weighs more than 100kg (150 > 100), so return ["Elephant"]
Example 2 — Multiple Heavy Animals
$ Input: animals = [{"name":"Rhino","species":"Rhinoceros","age":8,"weight":120},{"name":"Hippo","species":"Hippopotamus","age":6,"weight":180},{"name":"Elephant","species":"Elephantidae","age":12,"weight":150}]
Output: ["Hippo","Elephant","Rhino"]
💡 Note: All three animals weigh > 100kg. Sorted by weight descending: Hippo(180), Elephant(150), Rhino(120)
Example 3 — No Heavy Animals
$ Input: animals = [{"name":"Tiger","species":"Panthera tigris","age":4,"weight":90},{"name":"Lion","species":"Panthera leo","age":3,"weight":85}]
Output: []
💡 Note: No animals weigh more than 100kg (Tiger: 90kg, Lion: 85kg), so return empty array

Constraints

  • 1 ≤ animals.length ≤ 104
  • 1 ≤ weight ≤ 1000
  • name contains only letters and spaces

Visualization

Tap to expand
Method Chaining: Filter Heavy Animals and SortInput Animals DataFrame:Lion(80kg), Elephant(150kg), Tiger(90kg), Rhino(120kg), Hippo(180kg)Filter: weight > 100↓ Apply Filter Condition ↓Heavy Animals (weight > 100kg):Elephant(150kg), Rhino(120kg), Hippo(180kg)↓ Sort by Weight Descending ↓Sorted Result:Hippo(180) → Elephant(150) → Rhino(120)Output: ["Hippo", "Elephant", "Rhino"]
Understanding the Visualization
1
Input
DataFrame with animal records
2
Filter
Select animals with weight > 100kg
3
Sort & Extract
Sort by weight descending, return names
Key Takeaway
🎯 Key Insight: Use DataFrame method chaining to filter, sort, and select in one expression
Asked in
Meta 25 Google 20 Amazon 15
31.5K Views
Medium Frequency
~8 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen