Recyclable and Low Fat Products
Asked at Google
Difficulty
Easy
Time
O(n)
Space
O(1)
Pattern
Database
Companies
1
Problem
You are given a table called Products. Each row describes one product with three columns: product_id (a unique integer id), low_fats (either 'Y' or 'N'), and recyclable (either 'Y' or 'N').
Write a SQL query that returns the product_id of every product where both flags are 'Y' — that is, the product is low fat and recyclable. A product that is only one of the two must not appear in the result.
The result table has a single column, product_id. The rows may come back in any order, so no sorting is needed.
Keep only rows with Y in both columns
Constraints
- ●
product_id is the primary key of the Products table - ●
low_fats is an ENUM with values 'Y' or 'N' - ●
recyclable is an ENUM with values 'Y' or 'N' - ●
The result may be returned in any order
Examples
Products table:
+------------+----------+------------+
| product_id | low_fats | recyclable |
+------------+----------+------------+
| 0 | Y | N |
| 1 | Y | Y +------------+
| product_id |
+------------+
| 1 |
| 3 |
+------------+Products table:
+------------+----------+------------+
| product_id | low_fats | recyclable |
+------------+----------+------------+
| 7 | Y | Y |
+------------+----------+---+------------+
| product_id |
+------------+
| 7 |
+------------+Products table:
+------------+----------+------------+
| product_id | low_fats | recyclable |
+------------+----------+------------+
| 5 | Y | N |
+------------+----------+---+------------+
| product_id |
+------------+Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| Easy | View all Google questions → |
How to Think About It
Simple WHERE clause: filter on two boolean conditions. WHERE recyclable = "Y" AND low_fat = "Y". This is a straightforward O(n) scan of the Products table.
Column types: recyclable and low_fat are varchar with values "Y" or "N", not boolean. Compare with string literals, not 1/0 or true/false.
The query is just SELECT product_id FROM Products WHERE recyclable = "Y" AND low_fat = "Y". No joins, no aggregation, no subqueries. Pure filtering.
Edge cases: all products could be recyclable and low fat (return all ids), none could be (return empty), or the table could be empty (return empty). All handled by the same query.
Optimal Approach
Step 1: SELECT product_id from the Products table.
Step 2: WHERE clause filters recyclable = "Y" AND low_fat = "Y".
Step 3: Return all matching product_ids.
Walkthrough: For [[1, Apple, Y, N], [2, Banana, N, Y], [3, Orange, Y, Y]]:
- Product 1: recyclable=Y, low_fat=N -> fails
- Product 2: recyclable=N, low_fat=Y -> fails
- Product 3: recyclable=Y, low_fat=Y -> passes
- Result: [3]
Time: O(n) for a full table scan. Space: O(1) for the query execution.
Filter with one WHERE clause
What Trips People Up in Real Interviews
Comparing with boolean values instead of strings. recyclable = true won't work if the column stores "Y"/"N" as varchar. Check the schema carefully before writing the query.
Using OR instead of AND. The problem asks for products that are BOTH recyclable AND low fat. OR would return products that are recyclable OR low fat, which is a much larger set.
Selecting product name instead of product_id. The problem asks for ids, not names. SELECT product_id is correct.
Adding unnecessary DISTINCT. Each product_id is already unique as a primary key. DISTINCT is harmless but wasteful and shows you're not confident about the schema.
Forgetting that the table might have no matching rows. The query returns an empty result set, which is valid. Don't add a UNION with a fake row or anything creative.
Solution Code
O(n) timeSELECT product_id
FROM Products
WHERE recyclable = 'Y' AND low_fat = 'Y';Frequently Asked Questions
What is the Recyclable and Low Fat Products problem?
Write a SQL query to find the ids of products that are both recyclable and low fat. The Products table contains product_id, name, recyclable, and low_fat columns. This tests basic SQL filtering with multiple conditions.
How do you solve Recyclable and Low Fat Products?
The optimal approach runs in O(n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Recyclable and Low Fat Products?
Recyclable and Low Fat Products is asked at Google. It is a easy difficulty problem.
What are common mistakes on Recyclable and Low Fat Products?
- Comparing with boolean values instead of strings. `recyclable = true` won't work if the column stores "Y"/"N" as varchar. Check the schema carefully before writing the query.
- Using OR instead of AND. The problem asks for products that are BOTH recyclable AND low fat. `OR` would return products that are recyclable OR low fat, which is a much larger set.
- Selecting product name instead of product_id. The problem asks for ids, not names. `SELECT product_id` is correct.
- Adding unnecessary DISTINCT. Each product_id is already unique as a primary key. DISTINCT is harmless but wasteful and shows you're not confident about the schema.
- Forgetting that the table might have no matching rows. The query returns an empty result set, which is valid. Don't add a UNION with a fake row or anything creative.