Top Destination by Clear-Day Frequency
SQL & DataAuto-graded · 14 test casesLast updated
Tests grouping, filtering categorical records, and correctly handling ties when aggregating a frequency count across multiple keys.
Top Destination by Clear-Day Frequency
A travel analytics team maintains a daily log of weather observations for various destinations. Each record captures the destination name, the date of the observation, and a weather label for that day.
Your task is to process this log and identify which destination appears most often with the label "sunny". If multiple destinations share the highest count, return all of them. If no record in the entire log carries the label "sunny", return an empty list.
Input
A list of dictionaries. Each dictionary has exactly three string keys:
"country"— name of the destination"date"— observation date (format"YYYY-MM-DD")"weather"— weather label for that day (e.g."sunny","cloudy","rainy")
Output
A list of destination name strings representing every destination that has the maximum number of "sunny" observations. The order of names in the returned list should follow the order in which each destination first appears as a "sunny" entry in the input.
Notes
- Match the label with exact equality.
"Sunny"and"SUNNY"do not count. - Count rows, not distinct dates. If the same destination and date appear more than once, each row is counted independently.
- A destination that never has a
"sunny"row must never appear in the output, even if it has many rows with other labels. - An empty input or a log with no
"sunny"rows at all should produce[].
Constraints
0 ≤ len(weather) ≤ 10^5- Each dictionary always contains the keys
"country","date", and"weather". - All values are non-empty strings.
- Destination names are case-sensitive and may contain spaces.
- Date strings are well-formed but are not used in the computation.
Examples
Example 1
Input: top_destinations_by_clear_days([{'country': 'Spain', 'date': '2023-07-01', 'weather': 'sunny'}, {'country': 'Spain', 'date': '2023-07-02', 'weather': 'cloudy'}, {'country': 'Brazil', 'date': '2023-07-01', 'weather': 'sunny'}, {'country': 'Canada', 'date': '2023-07-01', 'weather': 'rainy'}, {'country': 'Brazil', 'date': '2023-07-02', 'weather': 'sunny'}])
Output: ['Brazil']
Hints
Hint 1
Start by ignoring every row whose weather label is not exactly "sunny". You only need to count those rows.
Hint 2
Use a dictionary keyed by destination name to accumulate the count of sunny rows. One linear pass over the filtered data is enough.
Hint 3
After building the counts, find the maximum value, then collect every destination whose count equals that maximum. This naturally handles ties without depending on any sort order.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The problem reduces to three steps: filter, aggregate, then find the maximum.
Step 1 — Filter. Ignore every row whose "weather" value is not exactly "sunny". This keeps destinations with zero sunny days out of the count map entirely, which is the cleanest way to handle the edge case where a destination should never appear in the output.
Step 2 — Aggregate. Walk the filtered rows and increment a counter keyed by destination name. A defaultdict(int) initialises each new key to zero automatically, so no special-casing is needed for the first occurrence.
Step 3 — Find the maximum and collect ties. Take max() over the counter values, then list every destination whose count equals that maximum. This returns all tied winners rather than an arbitrary single one, which is the correct and deterministic behaviour. Python dictionaries preserve insertion order, so the output order follows each destination's first sunny entry in the input.
Why it is correct.
- Destinations with no sunny rows are never inserted into the map, so they can never be returned.
- Comparing every count against the global maximum ensures no tied destination is silently dropped.
- If the entire log is empty or has no sunny rows,
sunny_countsstays empty, the early return fires, and the result is[].
Complexity.
- Time: O(n) for the single pass over the input, plus O(k) to scan the count map for the maximum and build the result, where k is the number of distinct destinations that have at least one sunny row. Overall O(n).
- Space: O(k) for the count map.
Edge cases to keep in mind.
- Empty input →
[]. - No
"sunny"rows at all →[]. - All destinations tied → all of them returned.
- Exact label matching:
"Sunny"or" sunny"are not counted. - Duplicate rows for the same destination and date are each counted independently.
from typing import List, Dict
from collections import defaultdict
def top_destinations_by_clear_days(weather: List[Dict[str, str]]) -> List[str]:
sunny_counts = defaultdict(int)
# Single pass: filter to sunny rows and count per destination
for record in weather:
if record['weather'] == 'sunny':
sunny_counts[record['country']] += 1
# No sunny observations anywhere
if not sunny_counts:
return []
max_count = max(sunny_counts.values())
# Return all destinations tied at the maximum, preserving insertion order
return [dest for dest, count in sunny_counts.items() if count == max_count]