Design a Weather Application (Live Coding + Architecture)
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:
🎯 Functional Requirements:
⚙️ Non-Functional Requirements:
🧱 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:
🧠 Bonus Concepts: