Skip to main content

Not Boring Movies - Solution & Explanation

EasyDatabase10 min readAsked at: Amazon, Microsoft, Meta +2
Practice this problem

Problem Statement

Table: Cinema

+----------------+----------+
| Column Name    | Type     |
+----------------+----------+
| id             | int      |
| movie          | varchar  |
| description    | varchar  |
| rating         | float    |
+----------------+----------+
id is the primary key (column with unique values) for this table.
Each row contains information about the name of a movie, its genre, and its rating.
rating is a 2 decimal places float in the range [0, 10]

 

Write a solution to report the movies with an odd-numbered ID and a description that is not "boring".

Return the result table ordered by rating in descending order.

The result format is in the following example.

 

Example 1:

Input: 
Cinema table:
+----+------------+-------------+--------+
| id | movie      | description | rating |
+----+------------+-------------+--------+
| 1  | War        | great 3D    | 8.9    |
| 2  | Science    | fiction     | 8.5    |
| 3  | irish      | boring      | 6.2    |
| 4  | Ice song   | Fantacy     | 8.6    |
| 5  | House card | Interesting | 9.1    |
+----+------------+-------------+--------+
Output: 
+----+------------+-------------+--------+
| id | movie      | description | rating |
+----+------------+-------------+--------+
| 5  | House card | Interesting | 9.1    |
| 1  | War        | great 3D    | 8.9    |
+----+------------+-------------+--------+
Explanation: 
We have three movies with odd-numbered IDs: 1, 3, and 5. The movie with ID = 3 is boring so we do not include it in the answer.

Approach Overview

Problem Overview: The cinema table contains movie records with id, movie, description, and rating. Return only movies whose id is odd and whose description is not 'boring'. The result must be sorted by rating in descending order.

Approach 1: SQL Query Using Simple Filtering (O(n log n) time, O(1) extra space)

This is the direct database solution. Filter rows with two conditions: id % 2 = 1 (odd IDs) and description != 'boring'. After filtering, apply ORDER BY rating DESC to sort the result. The database scans rows and performs a sort, giving roughly O(n log n) time due to ordering. This approach relies on standard SQL filtering and works cleanly in ORMs such as SQLAlchemy.

Approach 2: Query Using SQL Conditions (Entity Framework) (O(n log n) time, O(1) extra space)

ORM frameworks like Entity Framework translate high‑level conditions into SQL. You express the same logic using conditional filters: select rows where id % 2 != 0 and description != "boring", then order by rating descending. The ORM generates the SQL query and pushes computation to the database engine. Complexity remains O(n log n) due to sorting, while the query benefits from database indexes if present.

Approach 3: Divide and Conquer (O(n log n) time, O(n) space)

If the dataset is loaded into memory (for example in a service layer), you can split the list of movies into halves, filter each half recursively for valid rows, and merge the results. During the merge step, apply a descending sort by rating similar to merge sort. The divide‑and‑conquer structure keeps filtering independent per segment, and the final merge step maintains ordering. This resembles techniques used in divide and conquer algorithms.

Approach 4: Dynamic Programming (Tabulation) (O(n log n) time, O(n) space)

A tabulation approach processes rows sequentially and stores valid results in a table or list. For each movie, check two conditions: odd ID and non‑boring description. Valid rows are appended to the DP table, which represents the accumulated filtered set. After processing all rows, sort the stored entries by rating in descending order. While DP is unnecessary for this problem, the pattern demonstrates incremental filtering and aggregation often used in dynamic programming workflows.

Recommended for interviews: The straightforward SQL filtering approach is what interviewers expect. It shows you understand conditional selection (WHERE) and result ordering (ORDER BY). Alternative approaches like divide‑and‑conquer or DP demonstrate algorithmic thinking when data is handled outside the database, but the optimal production solution keeps filtering inside the SQL query.

Approach 1: SQL Query Using Simple Filtering

This approach involves using a straightforward SQL query to filter movies based on conditions specified in the question. We will look for movies with an ID that is odd and where the description is not 'boring'. Once filtered, results will be sorted according to the ratings in descending order.

We use SQLAlchemy to define a table 'Cinema' and insert the sample data. We query this table using conditions specified: id as odd and a non-'boring' description, finally ordering by ratings in descending order. The results are printed one by one.

Code

Python (SQLAlchemy)

Complexity

The time complexity of this approach is O(n log n) due to sorting of the movies after filtering, and space complexity is O(1) as we do not use extra space apart from input size.

Try this approach in the editor →

Approach 2: Query Using SQL Conditions

This approach directly uses SQL logic to filter out the records based on conditions specified in the question: selecting only those entries with an odd-numbered ID and a non-boring description, followed by sorting the result set based on ratings in descending order.

We begin by defining a Movie class and a list of Movie objects, after which we filter through this list in a LINQ query to fetch movies with odd-numbered IDs while excluding descriptions marked 'boring'. Finally, results are ordered by rating before being printed.

Code

C# (Entity Framework)

Complexity

The time complexity of this approach is O(n log n) due to sorting, and the space complexity is O(n) because we store the list of filtered movies.

Try this approach in the editor →

Approach 3: Divide and Conquer

This approach involves breaking down the problem into smaller sub-problems, solving each one individually, and then combining their solutions to solve the original problem. This is a powerful technique used in algorithms like Merge Sort and Quick Sort. It allows us to efficiently tackle the problem by utilizing recursive problem-solving.

This is a Merge Sort implementation in C, which follows the Divide and Conquer technique. The array is recursively split into halves until each sub-array has one element. Then, the sub-arrays are merged back together in a sorted manner. This algorithm is efficient for large datasets.

Code

C

Python

Complexity

Time Complexity: O(n log n)
Space Complexity: O(n)

Try this approach in the editor →

Approach 4: Dynamic Programming (Tabulation)

Dynamic programming is a technique used for solving complex problems by breaking them down into simpler subproblems. Tabulation involves solving the subproblems and storing their results in a table (usually an array) to avoid redundant calculations. This approach is efficient for problems where overlapping subproblems and optimal substructure properties exist.

This JavaScript function calculates the nth Fibonacci number using a bottom-up dynamic programming approach. It uses an array to store and reuse previously computed Fibonacci numbers, efficiently computing the result in O(n) time.

Code

JavaScript

Java

Complexity

Time Complexity: O(n)
Space Complexity: O(n)

Try this approach in the editor →

Approach 5: Conditional Filtering + Sorting

We can use the WHERE clause to filter out the records where description is not boring and id is odd, and then use the ORDER BY clause to sort the result in descending order by rating.

Code

MySQL

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
SQL Query Using Simple Filtering

The time complexity of this approach is O(n log n) due to sorting of the movies after filtering, and space complexity is O(1) as we do not use extra space apart from input size.

Query Using SQL Conditions

The time complexity of this approach is O(n log n) due to sorting, and the space complexity is O(n) because we store the list of filtered movies.

Divide and Conquer

Time Complexity: O(n log n)
Space Complexity: O(n)

Dynamic Programming (Tabulation)

Time Complexity: O(n)
Space Complexity: O(n)

Conditional Filtering + Sorting—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
SQL Query Using Simple FilteringO(n log n)O(1)Best approach when querying directly from a database using SQL
Query Using SQL Conditions (ORM)O(n log n)O(1)When using frameworks like Entity Framework or SQLAlchemy
Divide and ConquerO(n log n)O(n)When the dataset is processed in memory and parallel filtering is desired
Dynamic Programming (Tabulation)O(n log n)O(n)When building filtered results incrementally before final sorting

Video Solution

Not Boring Movies | Leetcode 620 | Crack SQL Interviews in 50 Qs #mysql #leetcode • Learn With Chirag • 10,261 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Not Boring Movies easy or hard?
Not Boring Movies is classified as Easy. The task only requires understanding SQL WHERE filters, modulus checks for odd IDs, and ORDER BY sorting. It is often used to test basic SQL query writing skills.
Not Boring Movies Python/Java solution
In Python, frameworks like SQLAlchemy express the solution using filters on id and description followed by ordering by rating descending. In Java or C#, ORMs such as Entity Framework generate equivalent SQL queries with the same conditions and sorting logic.
How to solve Not Boring Movies in O(n)?
Pure O(n) is possible only if the database already stores rows indexed or pre‑sorted by rating. Otherwise sorting dominates the complexity. The standard solution scans rows with WHERE conditions and then sorts by rating, giving O(n log n) time in typical implementations.
What is the best approach for Not Boring Movies?
The best approach is a simple SQL query with filtering and sorting. Use WHERE conditions to keep rows where id is odd (id % 2 = 1) and description is not 'boring', then apply ORDER BY rating DESC. This pushes all work to the database engine and runs in roughly O(n log n) time due to sorting.
Is Not Boring Movies asked at Google/Amazon/Meta?
Not Boring Movies is a common introductory SQL filtering problem similar to questions used in database screening rounds. Variants of conditional filtering and ordering appear in interviews at large tech companies including Amazon and Google when testing SQL fundamentals.
What data structure is used in Not Boring Movies?
The problem operates on a relational database table. Conceptually it works on rows in a table structure where filtering conditions select valid records and a sorting operation orders them by rating.
What is the time complexity of Not Boring Movies?
The query typically runs in O(n log n) time because the database must sort the filtered rows by rating. The filtering step itself is O(n). Extra space is O(1) from the query perspective since the database engine manages intermediate memory internally.

Ready to solve this problem?

Practice Not Boring Movies with our built-in code editor and test cases.

Practice on FleetCode