
BAA
Mobile apps depend on remote APIs, but networks are unreliable, latency is variable, and users expect responsive screens. Interviewers want to see whether you can design a client-side integration that is correct, resilient, and maintainable.
Explain how you would handle API integration in a mobile app.
Address these points:
Focus on the client-side engineering approach rather than backend implementation details. A strong answer should cover request flow, state management, error handling, and practical trade-offs for performance and reliability.
A mobile app should isolate API calls in a dedicated networking or repository layer instead of calling endpoints directly from UI code. This improves testability, keeps view logic simple, and makes authentication, retries, and logging consistent across the app.
class UserRepository:
def __init__(self, api_client, cache):
self.api_client = api_client
self.cache = cache
Caching reduces latency and lets the app show useful data when the network is slow or unavailable. A common pattern is to return cached data immediately, then refresh in the background and update the UI if newer data arrives.
cached = cache.get(key)
if cached is not None:
return cached
Transient failures such as timeouts or temporary connectivity loss should not immediately surface as permanent errors. Use request timeouts and limited retries with exponential backoff, but avoid retrying non-idempotent operations blindly.
delay = base * (2 ** attempt)
Multiple screens or repeated taps can trigger the same API call at the same time. Deduplicating in-flight requests avoids wasted bandwidth, inconsistent state, and unnecessary battery use.
if key in in_flight:
return in_flight[key]
The UI should react to explicit states such as loading, success, empty, stale, and error. Modeling these states clearly prevents ambiguous behavior and helps the app show accurate feedback, retry actions, or cached fallback content.
state = {'status': 'loading', 'data': None, 'error': None}