KatInterview

Top Destination by Clear-Day Frequency

SQL & DataAuto-graded · 14 test cases
MediumAuto-gradedSQL & Dataaggregationgroupingfiltering

Last 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.