Practice/Meta/Design a Mobile Media Download Library
Design a Mobile Media Download Library
Product DesignOptional
Problem Statement
You are tasked with designing a client-side library for mobile applications that handles downloading various types of media files including images, videos, and audio tracks from remote servers. This library will be integrated directly into mobile apps (iOS/Android) and must handle the unique constraints of mobile environments: limited memory, intermittent connectivity, battery efficiency concerns, and the need to provide a seamless user experience even with slow or unreliable networks.
The library should support concurrent downloads, provide progress updates to the host application, handle network failures gracefully with retry logic, and efficiently manage device storage and memory. Unlike a distributed backend system, your focus here is on the client-side architecture including in-memory data structures, threading models, caching strategies, and integration patterns that mobile developers would use in their applications.
Key Requirements
Functional
- Multi-format support -- handle downloads for images (JPEG, PNG, WebP), video (MP4, HLS), and audio (MP3, AAC) formats
- Concurrent downloads -- allow multiple files to download simultaneously with configurable priority levels
- Progress tracking -- provide real-time progress callbacks to the host application for each download
- Resume capability -- support pausing and resuming downloads, especially for large video files
- Cache management -- implement intelligent caching with configurable size limits and eviction policies
- Request queuing -- queue download requests when connection is unavailable and process when network returns
Non-Functional
- Performance -- support at least 10-20 concurrent downloads without degrading device performance
- Memory efficiency -- limit in-memory buffers to avoid OOM crashes, stream large files to disk
- Battery optimization -- batch network requests and avoid keeping radio active unnecessarily
- Reliability -- handle network failures with exponential backoff, automatic retries up to configurable limits
- Thread safety -- ensure all public APIs are thread-safe for use from any application thread
What Interviewers Focus On
Based on real interview experiences, these are the areas interviewers probe most deeply:
1. Threading and Concurrency Model
Mobile applications require non-blocking APIs to keep the UI responsive. Interviewers want to see how you design the threading architecture to handle multiple concurrent downloads without blocking the main thread or consuming excessive resources.
Hints to consider:
- Use a dedicated download thread pool with size limits based on device capabilities
- Implement a serial dispatch queue per download to handle state transitions safely
- Consider how you'll notify the UI thread with progress updates without overwhelming it
- Think about thread synchronization mechanisms for shared resources like the cache and request queue
2. Network Error Handling and Retry Logic
Mobile networks are inherently unreliable with frequent disconnections, switching between WiFi and cellular, and varying bandwidth. How you handle these scenarios determines the robustness of your library.
Hints to consider:
- Implement exponential backoff with jitter to avoid thundering herd problems
- Use HTTP range requests to support resuming partial downloads
- Differentiate between retryable errors (network timeout) and non-retryable errors (404, 403)
- Consider user preferences like "download only on WiFi" and battery level thresholds
3. Memory Management and Streaming
Large video files can't be loaded entirely into memory. Interviewers expect you to understand streaming I/O patterns and how to efficiently write data to disk while keeping memory footprint minimal.
Hints to consider:
- Use fixed-size circular buffers to stream data from network to disk
- Implement chunked downloading to write data incrementally rather than buffering entire responses
- Consider memory-mapped files for efficient disk I/O on larger downloads
- Design proper resource cleanup to prevent memory leaks in long-running downloads
4. Caching Strategy and Persistence
The cache is critical for performance and user experience. Your design needs to handle both in-memory caching for frequently accessed small files and disk caching for larger media.
Hints to consider:
- Implement a two-tier cache: memory cache (LRU) for small images, disk cache for all media
- Use cache keys that account for URL, headers, and any authentication tokens
- Consider cache coherency strategies like ETags or Last-Modified headers
- Design an eviction policy based on LRU, file size, access frequency, and expiration times
5. Public API Design and Integration Patterns
As a library, your API design determines how easy it is for developers to integrate. Interviewers look for clean abstractions that hide complexity while providing necessary control.
Hints to consider:
- Provide both callback-based and reactive (RxJava, Combine) interfaces for different integration styles
- Design request objects with builder pattern for configurable options (priority, cache policy, headers)
- Consider how developers will handle download lifecycle in relation to Activity/ViewController lifecycle
- Think about dependency injection patterns for configuration like base URLs and authentication