LeetCode 595 - Big Countries
This problem asks us to query a database table named World and return a subset of countries that qualify as big countries.
Difficulty: 🟢 Easy
Topics: Database
Solution
Problem Understanding
This problem asks us to query a database table named World and return a subset of countries that qualify as big countries.
Each row in the World table represents a country and contains five columns:
| Column | Meaning |
|---|---|
name |
Country name |
continent |
Continent where the country belongs |
area |
Total land area in square kilometers |
population |
Number of people living in the country |
gdp |
Gross Domestic Product |
A country is considered big if either of the following conditions is true:
- The country has an area of at least
3,000,000 km² - The country has a population of at least
25,000,000
The important word here is or, which means satisfying either condition is enough for a country to qualify.
The goal is to return a table containing only the following columns:
namepopulationarea
for every country that satisfies the condition.
The problem explicitly states that the result can be returned in any order, meaning we do not need to sort the output.
From a database perspective, this is a straightforward filtering problem. We are simply selecting rows from a table where at least one condition holds true.
Because name is the primary key, we know each country appears exactly once. There are no duplicate rows to worry about.
The main edge case is understanding the threshold correctly. The conditions use at least, which means the comparison must be inclusive:
area >= 3000000population >= 25000000
Using strict greater-than (>) instead of greater-than-or-equal (>=) would produce incorrect results.
Another important edge case is countries that satisfy only one condition. For example:
- A country with a small area but very large population should still be included.
- A country with a large area but smaller population should also be included.
Finally, countries failing both conditions must be excluded.
Approaches
Brute Force Approach
The brute-force approach is to scan every row in the World table one by one and manually check whether the country satisfies either of the two requirements.
For each country:
- Check if
area >= 3000000 - Check if
population >= 25000000 - Include the row if either condition is true
This works because every row is evaluated independently, and we directly apply the exact definition of a big country.
In practice, relational databases already optimize this kind of filtering internally. Since we must inspect rows to determine whether they satisfy the condition, scanning the table is unavoidable.
Key Insight for the Optimal Solution
The key observation is that this is simply a filtering query.
SQL is designed specifically for this kind of operation. Instead of manually iterating over rows, we can express the logic declaratively using a WHERE clause.
The WHERE condition directly mirrors the problem definition:
area >= 3000000 OR population >= 25000000
Then we select only the required columns:
SELECT name, population, area
This is optimal because databases are optimized for filtering operations and can leverage indexing or execution plans internally if available.
| Approach | Time Complexity | Space Complexity | Notes |
|---|---|---|---|
| Brute Force | O(n) | O(1) | Scan every row and manually check conditions |
| Optimal | O(n) | O(1) | SQL filtering using WHERE clause |
Here, n represents the number of rows in the World table.
Algorithm Walkthrough
- Read rows from the
Worldtable.
Every country must be examined because any country could potentially satisfy the conditions. 2. Apply the filtering condition.
For each row, check whether:
area >= 3000000population >= 25000000
Since the problem uses or, a country is included if either condition is true. 3. Select only the required columns.
The problem asks for only:
namepopulationarea
Even though other columns exist in the table, they are not part of the output. 4. Return the resulting rows.
Since ordering does not matter, no sorting step is required.
Why it works
The solution works because the WHERE clause exactly matches the mathematical definition of a big country given in the problem statement. Every country is checked against both required conditions, and a row is returned if at least one condition evaluates to true. Since we select only the requested columns, the output format is guaranteed to match the specification.
Python Solution
LeetCode Database problems use SQL rather than standard Python classes. Below is the correct SQL solution expressed in a Python code block for formatting consistency.
SELECT
name,
population,
area
FROM World
WHERE area >= 3000000
OR population >= 25000000;
The implementation starts by selecting the required columns: name, population, and area. This ensures the output matches the requested schema exactly.
The FROM World clause specifies the source table.
The filtering logic is implemented in the WHERE clause. A country is included if its area is at least 3,000,000 or its population is at least 25,000,000. Because the problem defines the requirement using an inclusive threshold, the comparison operators use >=.
No sorting logic is included because the problem explicitly allows the result to be returned in any order.
Go Solution
LeetCode Database problems do not require Go code because the solution must be written in SQL. However, if we conceptually model the same logic in Go for educational purposes, it would look like this:
package main
type Country struct {
Name string
Continent string
Area int
Population int
GDP int64
}
type Result struct {
Name string
Population int
Area int
}
func bigCountries(world []Country) []Result {
result := []Result{}
for _, country := range world {
if country.Area >= 3000000 || country.Population >= 25000000 {
result = append(result, Result{
Name: country.Name,
Population: country.Population,
Area: country.Area,
})
}
}
return result
}
The Go version mirrors the SQL logic using a loop. Each country is checked against the same two conditions. If either condition is true, the required fields are added to the result slice.
Unlike SQL, Go requires us to explicitly define data structures and iterate through the dataset manually. The || operator represents logical OR, matching the problem definition exactly.
Since integer values such as GDP may exceed standard 32-bit integer limits, int64 is used for GDP. The algorithm otherwise remains identical.
Worked Examples
Example 1
Input table:
| name | continent | area | population | gdp |
|---|---|---|---|---|
| Afghanistan | Asia | 652230 | 25500100 | 20343000000 |
| Albania | Europe | 28748 | 2831741 | 12960000000 |
| Algeria | Africa | 2381741 | 37100000 | 188681000000 |
| Andorra | Europe | 468 | 78115 | 3712000000 |
| Angola | Africa | 1246700 | 20609294 | 100990000000 |
We evaluate each row one by one.
| Country | Area Check (>=3000000) |
Population Check (>=25000000) |
Included? |
|---|---|---|---|
| Afghanistan | False | True | Yes |
| Albania | False | False | No |
| Algeria | False | True | Yes |
| Andorra | False | False | No |
| Angola | False | False | No |
Final result:
| name | population | area |
|---|---|---|
| Afghanistan | 25500100 | 652230 |
| Algeria | 37100000 | 2381741 |
Afghanistan qualifies because its population exceeds 25,000,000, even though its area is below 3,000,000.
Algeria also qualifies because its population exceeds the threshold.
The remaining countries fail both conditions and are excluded.
Complexity Analysis
| Measure | Complexity | Explanation |
|---|---|---|
| Time | O(n) | Every row in the table is checked once |
| Space | O(1) | No extra memory proportional to input size is required |
The database must inspect each row to determine whether the conditions are satisfied, leading to linear time complexity. Since the query only filters rows and does not allocate additional data structures proportional to the input size, the extra space complexity is constant.
Test Cases
def big_countries(world):
result = []
for country in world:
if country["area"] >= 3000000 or country["population"] >= 25000000:
result.append({
"name": country["name"],
"population": country["population"],
"area": country["area"],
})
return result
# Example test case from problem statement
world = [
{"name": "Afghanistan", "area": 652230, "population": 25500100},
{"name": "Albania", "area": 28748, "population": 2831741},
{"name": "Algeria", "area": 2381741, "population": 37100000},
]
assert big_countries(world) == [
{"name": "Afghanistan", "population": 25500100, "area": 652230},
{"name": "Algeria", "population": 37100000, "area": 2381741},
] # Provided example
# Area threshold exactly met
world = [
{"name": "CountryA", "area": 3000000, "population": 1},
]
assert big_countries(world) == [
{"name": "CountryA", "population": 1, "area": 3000000}
] # Inclusive area boundary
# Population threshold exactly met
world = [
{"name": "CountryB", "area": 1, "population": 25000000},
]
assert big_countries(world) == [
{"name": "CountryB", "population": 25000000, "area": 1}
] # Inclusive population boundary
# Country satisfies both conditions
world = [
{"name": "CountryC", "area": 5000000, "population": 50000000},
]
assert big_countries(world) == [
{"name": "CountryC", "population": 50000000, "area": 5000000}
] # Both conditions true
# No country qualifies
world = [
{"name": "CountryD", "area": 1000, "population": 1000},
]
assert big_countries(world) == [] # Empty result
# Empty input
world = []
assert big_countries(world) == [] # No rows to process
| Test | Why |
|---|---|
| Problem example | Validates correctness against official example |
| Exact area threshold | Confirms >= is used instead of > |
| Exact population threshold | Verifies inclusive population boundary |
| Both conditions true | Ensures OR logic works correctly |
| No qualifying country | Confirms invalid rows are excluded |
| Empty input | Verifies graceful handling of no rows |
Edge Cases
One important edge case is a country whose area or population is exactly equal to the threshold. Because the problem says at least, equality should count. Using strict greater-than comparisons would incorrectly exclude valid countries. The implementation handles this correctly using >=.
Another important case is countries satisfying only one condition. A naive implementation might accidentally require both conditions by using AND instead of OR. The solution explicitly uses logical OR, ensuring countries qualify even if only area or only population exceeds the threshold.
A third edge case is an empty table. If there are no countries in the input, the query should simply return an empty result set. SQL naturally handles this case without special logic, and the conceptual Python and Go implementations return an empty collection as expected.