Weather Observation Station 5 Problem

The problem context involves a database table named 'station' which contains a 'city' field. The task is to identify the city with the shortest name and the city with the longest name. If there are multiple cities with the same shortest or longest name, the city with the name that comes first in alphabetical order should be selected.

Secure your next interview 🎯

CodeRankGPT is a tool powered by GPT-4 that quietly assists you during your coding interview, providing the solutions you need.
In real-time and absolutely undetectable 🥷

Here is the Weather Observation Station 5 Problem HackerRank solution using CodeRankGPT:

The solution approach involves two SQL queries. The first query selects the 'city' and its length from the 'station' table, orders the results by the length of the city name in ascending order and then by the city name itself in ascending order, and finally limits the results to the first entry. This returns the city with the shortest name. The second query does the same but orders the results by the length of the city name in descending order, thus returning the city with the longest name. The 'distinct' keyword is used to ensure that each city name is only considered once.


select city,length(city) from station order By length(city) asc, city asc limit 1;
select distinct(City),length(city) from station order by length(city) desc, city asc limit 1;

If you have a HackerRank coding test coming up, you can use CodeRankGPT to your advantage. It will assist you during your interview and help ensure you get the job.

AI is here now, and other candidates might be using it to get ahead and win the job. 🧐

Built on Unicorn Platform