DataFrame animals
+-------------+--------+
| Column Name | Type |
+-------------+--------+
| name | object |
| species | object |
| age | int |
| weight | int |
+-------------+--------+
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.
The result format is in the following example.
Example 1:
Input: DataFrame animals: +----------+---------+-----+--------+ | name | species | age | weight | +----------+---------+-----+--------+ | Tatiana | Snake | 98 | 464 | | Khaled | Giraffe | 50 | 41 | | Alex | Leopard | 6 | 328 | | Jonathan | Monkey | 45 | 463 | | Stefan | Bear | 100 | 50 | | Tommy | Panda | 26 | 349 | +----------+---------+-----+--------+ Output: +----------+ | name | +----------+ | Tatiana | | Jonathan | | Tommy | | Alex | +----------+ Explanation: All animals weighing more than 100 should be included in the results table. Tatiana's weight is 464, Jonathan's weight is 463, Tommy's weight is 349, and Alex's weight is 328. The results should be sorted in descending order of weight.
In Pandas, method chaining enables us to perform operations on a DataFrame without breaking up each operation into a separate line or creating multiple temporary variables.
Can you complete this task in just one line of code using method chaining?
Problem Overview: The task models a sequence of operations that behave like chained method calls. Each step depends on the previous result, so you must process the operations in order and maintain the current state correctly while keeping the implementation efficient.
Approach 1: Using a HashMap or Dictionary (O(n) time, O(n) space)
This approach stores intermediate state or operation mappings in a HashMap (or dictionary). As you iterate through the sequence, each operation updates the current state using constant-time hash lookups. The key idea is that a map lets you quickly retrieve or update values associated with specific method names or identifiers without scanning the entire structure. This approach works well when the problem involves tracking values, resolving repeated operations, or aggregating results across the chain. Hash lookups keep updates at O(1) on average, leading to an overall O(n) traversal of the operations.
Hash maps are a common tool for problems that require quick lookups or state tracking. If you're reviewing related patterns, see hash map techniques frequently used in many array and string problems.
Approach 2: Two-Pointer Technique (O(n) time, O(1) space)
The two-pointer technique processes the chain from both ends of the sequence. One pointer starts at the beginning while the other starts at the end, and both move inward depending on how operations interact. This works particularly well when opposite operations can cancel out or when evaluating the chain requires comparing elements from both sides. Because you only maintain a few indices and update them as you scan, the extra memory usage stays constant.
This method avoids the overhead of additional data structures and is often faster in practice due to better cache locality. It is a standard pattern in problems involving ordered data or symmetric processing. If you want more practice with this pattern, review two pointer problems and related array traversal strategies.
Recommended for interviews: Start with the HashMap-based solution to show you understand how to manage state and perform constant-time lookups during the chain evaluation. Then discuss the two-pointer optimization if the operations allow symmetric processing. Interviewers typically prefer the O(n) time, O(1) space two-pointer approach when applicable because it demonstrates stronger algorithmic reasoning and space optimization.
This approach leverages a hash map (or dictionary) to solve the problem efficiently. By taking advantage of the average O(1) time complexity for insert and lookup operations in hash maps, we can create a mapping between elements and their indices (or frequencies, depending on the problem requirements). This method not only offers efficient retrieval but also makes it easier to track elements as we iterate through the data structure.
This Python solution iterates over the list of numbers while maintaining a hash map of previously visited numbers and their indices. For each number, it calculates the complement (i.e., the difference between the target and the current number). If the complement is found in the hash map, the indices of the current number and the complement are returned as they sum to the target.
Time Complexity: O(n), where n is the number of elements in the input list.
Space Complexity: O(n), due to the storage of numbers in the hash map.
This approach utilizes a two-pointer technique which is particularly effective when the input is sorted (or can be sorted) without significantly impacting performance. By using two pointers to traverse the array from both ends, we can efficiently find the pair of elements that sum to the target. Note that this approach is based on the assumption that sorting the input is feasible and will not exceed time limits.
This Python implementation first sorts the list while retaining the original indices. Two pointers are then used to check for the target sum from the front and back of the sorted list. If the sum is less than the target, the left pointer is moved forward; otherwise, the right pointer is moved backward.
Python
JavaScript
Java
C++
C#
Time Complexity: O(n log n) due to sorting.
Space Complexity: O(n) because we store tuples of indices and numbers.
Python
| Approach | Complexity |
|---|---|
| Using a HashMap or Dictionary | Time Complexity: O(n), where n is the number of elements in the input list. |
| Two-Pointer Technique | Time Complexity: O(n log n) due to sorting. |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| HashMap / Dictionary | O(n) | O(n) | General case when you need fast lookups or must track intermediate state for operations in the chain |
| Two-Pointer Technique | O(n) | O(1) | When operations can be evaluated symmetrically or canceled from both ends of the sequence |
Leetcode: 2891. Method Chaining • Donutloop • 450 views views
Watch 6 more video solutions →Practice Method Chaining with our built-in code editor and test cases.
Practice on FleetCode