{x}
blog image

Recyclable and Low Fat Products

Table: Products

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| product_id  | int     |
| low_fats    | enum    |
| recyclable  | enum    |
+-------------+---------+
product_id is the primary key (column with unique values) for this table.
low_fats is an ENUM (category) of type ('Y', 'N') where 'Y' means this product is low fat and 'N' means it is not.
recyclable is an ENUM (category) of types ('Y', 'N') where 'Y' means this product is recyclable and 'N' means it is not.

 

Write a solution to find the ids of products that are both low fat and recyclable.

Return the result table in any order.

The result format is in the following example.

 

Example 1:

Input: 
Products table:
+-------------+----------+------------+
| product_id  | low_fats | recyclable |
+-------------+----------+------------+
| 0           | Y        | N          |
| 1           | Y        | Y          |
| 2           | N        | Y          |
| 3           | Y        | Y          |
| 4           | N        | N          |
+-------------+----------+------------+
Output: 
+-------------+
| product_id  |
+-------------+
| 1           |
| 3           |
+-------------+
Explanation: Only products 1 and 3 are both low fat and recyclable.

Solution Explanation:

The problem requires retrieving product_ids from the Products table where both low_fats and recyclable columns have the value 'Y'. This is a simple filtering task achievable using SQL or Pandas.

Approach:

Both solutions employ a straightforward filtering approach. They check for the desired conditions (low_fats = 'Y' and recyclable = 'Y') and select only the matching product_ids.

Code Explanation:

Python (using Pandas):

The Python solution utilizes the Pandas library for data manipulation.

import pandas as pd
 
def find_products(products: pd.DataFrame) -> pd.DataFrame:
    rs = products[(products["low_fats"] == "Y") & (products["recyclable"] == "Y")]
    rs = rs[["product_id"]]
    return rs
  1. import pandas as pd: Imports the Pandas library.
  2. def find_products(products: pd.DataFrame) -> pd.DataFrame:: Defines a function that takes a Pandas DataFrame (products) as input and returns another Pandas DataFrame.
  3. rs = products[(products["low_fats"] == "Y") & (products["recyclable"] == "Y")]: This line performs the core filtering. It creates a boolean mask by comparing the low_fats and recyclable columns to 'Y'. The & operator ensures both conditions must be true. The resulting boolean mask is then used to select only the rows that satisfy both conditions.
  4. rs = rs[["product_id"]]: This line selects only the product_id column from the filtered DataFrame.
  5. return rs: The function returns the DataFrame containing only the product_ids of products that are both low-fat and recyclable.

MySQL:

The MySQL solution is a concise SQL query:

SELECT
    product_id
FROM Products
WHERE low_fats = 'Y' AND recyclable = 'Y';
  1. SELECT product_id: Specifies that only the product_id column should be returned.
  2. FROM Products: Indicates that the data should be retrieved from the Products table.
  3. WHERE low_fats = 'Y' AND recyclable = 'Y': This is the filtering condition. It selects rows where both low_fats and recyclable columns are equal to 'Y'.

Time Complexity Analysis:

Both solutions have a time complexity of O(n), where n is the number of rows in the Products table. This is because both the Pandas filtering and the SQL WHERE clause need to iterate (or at least perform a scan) through the table to identify matching rows. The filtering operation itself is linear in the number of rows.

Space Complexity Analysis:

The space complexity of both solutions is O(k) where k is the number of rows that satisfy the condition. In the worst case, k could be equal to n, but in many practical scenarios, k will be significantly smaller than n. The Python solution uses extra space for the creation of the resulting Pandas DataFrame, while the SQL solution's space usage is determined by the size of the result set.