Design a Weather Application (Live Coding + Architecture)

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2025-03-19

Problem Statement

Design and implement a Weather App that allows users to view current weather conditions and forecasts for various locations. The app should fetch data asynchronously from a weather API, display it using UITableView, and be designed in a modular, scalable, and performant way.


You may be asked to live-code parts of the solution, such as:

•Making asynchronous API calls
•Parsing JSON response
•Populating UITableView
•Using URLSession, Codable, and DispatchQueue.main
•Basic error handling


🎯 Functional Requirements:

1.City Search
•User can search and add cities to monitor weather
2.Weather Details
•Current weather (temperature, condition, humidity, wind)
•Forecast (next 5 days)
3.Asynchronous Data Fetching
•Use URLSession to call weather APIs
4.UI Display
•Use UITableView to display weather data
5.Pull to Refresh
•Refresh weather data manually


⚙️ Non-Functional Requirements:

•Smooth, responsive UI (async rendering)
•Clean architecture (MVC or MVVM)
•Error messages for failed requests
•Minimal latency in displaying fetched data
•Battery/network efficient


🧱 Suggested System Design (Mobile Architecture)

🔹 App Layers:

LayerResponsibility

Model

Weather data structures (City, Weather, Forecast)

View

Storyboard or SwiftUI with UITableView

Controller / ViewModel

Handles networking, parses response, updates UI

Networking Layer

Makes async API calls to OpenWeatherMap / similar


📦 Sample Data Models (Swift)

1swift 2CopyEdit 3struct WeatherResponse: Codable { 4 let city: String 5 let temperature: Double 6 let condition: String 7 let forecast: [Forecast] 8} 9 10struct Forecast: Codable { 11 let date: String 12 let temp: Double 13 let condition: String 14}


🔌 Networking Layer (Async)

1swift 2CopyEdit 3func fetchWeather(for city: String, completion: @escaping (WeatherResponse?) -> Void) { 4 let url = URL(string: "https://api.weatherapi.com/v1/current.json?key=API_KEY&q=\(city)")! 5 6 URLSession.shared.dataTask(with: url) { data, response, error in 7 guard let data = data else { 8 completion(nil) 9 return 10 } 11 12 do { 13 let weather = try JSONDecoder().decode(WeatherResponse.self, from: data) 14 DispatchQueue.main.async { 15 completion(weather) 16 } 17 } catch { 18 print("Parsing error: \(error)") 19 DispatchQueue.main.async { 20 completion(nil) 21 } 22 } 23 }.resume() 24}


📋 UITableView Methods (Swift)

1swift 2CopyEdit 3// MARK: - UITableViewDataSource 4func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 5 return forecasts.count 6} 7 8func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 9 let forecast = forecasts[indexPath.row] 10 let cell = tableView.dequeueReusableCell(withIdentifier: "ForecastCell", for: indexPath) 11 cell.textLabel?.text = "\(forecast.date): \(forecast.temp)°C - \(forecast.condition)" 12 return cell 13}


🧪 Possible Live Coding Questions:

•Implement fetchWeather() with URLSession.
•Create UITableViewCell to display forecast data.
•Update UI from async callbacks.
•Handle JSON parsing failures.
•Add UIRefreshControl for pull-to-refresh behavior.
•Discuss MVVM usage to separate logic from UI.


🧠 Bonus Concepts:

•Use Combine or async/await (if using Swift 5.5+)
•Cache last fetched weather for offline use
•Implement location-based weather (CoreLocation)
•Show loading indicator while fetching data
•Unit test the networking and parsing logic


Solution

Comments