-
๐ Welcome, Iโm Shobhakar Tiwari working in the USA ๐บ๐ธ and currently in the Central Standard Time Zone (CST). I enjoy writing blogs on Medium, contributing to the iOS dev community, and sharing iOS interview questions to support junior and senior iOS developers alike.
-
If you preparing for iOS Interview feel free to connect for mentorship.
-
A comprehensive collection of real-world technical interview questions and scenarios from leading technology companies. This repository helps developers prepare for coding rounds, system design interviews, and technical assessments.
Real-time ETA Tracking in Ride-Sharing Application
Design and implement a feature for a ride-sharing application that displays real-time ETA (Estimated Time of Arrival) updates for drivers. The system must efficiently handle periodic network requests while maintaining optimal resource usage and user experience.
- Implement periodic ETA fetching (10-second intervals)
- Handle view lifecycle management
- Implement proper network request management
- Ensure thread-safe UI updates
- Implement memory leak prevention
- Handle error cases and network failures
- Timer System: Manages periodic network requests
- Network Layer: Handles API communication
- UI Layer: Updates and manages view state
- Error Handling: Implements retry mechanism with exponential backoff
- Resource optimization during screen transitions
- Thread safety in UI updates
- Memory management
- Network failure resilience
import UIKit
class ETAViewController: UIViewController {
// MARK: - Properties
private var timer: Timer?
private let etaURL = URL(string: "https://api.example.com/driver/eta")!
// MARK: - Lifecycle Methods
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
startFetchingETA()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
stopFetchingETA()
}
// MARK: - Private Methods
private func startFetchingETA() {
timer = Timer.scheduledTimer(
timeInterval: 10.0,
target: self,
selector: #selector(fetchETA),
userInfo: nil,
repeats: true
)
timer?.fire()
}
private func stopFetchingETA() {
timer?.invalidate()
timer = nil
}
@objc private func fetchETA() {
let task = URLSession.shared.dataTask(with: etaURL) { [weak self] data, response, error in
guard let self = self else { return }
if let error = error {
print("Failed to fetch ETA: \(error)")
return
}
guard let data = data else { return }
if let eta = self.parseETA(from: data) {
DispatchQueue.main.async {
self.updateUI(with: eta)
}
}
}
task.resume()
}
private func parseETA(from data: Data) -> String? {
return String(data: data, encoding: .utf8)
}
private func updateUI(with eta: String) {
print("ETA Updated: \(eta)")
}
}
Implement an exponential backoff strategy for handling network failures:
private func retryRequest(after delay: TimeInterval) {
DispatchQueue.main.asyncAfter(deadline: .now() + delay) { [weak self] in
self?.fetchETA()
}
}
Ensure UI updates occur on the main thread:
DispatchQueue.main.async {
self.updateUI(with: eta)
}
Implement proper request cancellation to prevent resource leaks:
private var currentTask: URLSessionDataTask?
@objc private func fetchETA() {
currentTask?.cancel()
currentTask = URLSession.shared.dataTask(with: etaURL) { [weak self] data, response, error in
// Handle response
}
currentTask?.resume()
}
We welcome contributions! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.