You’re building the image pipeline for a high-traffic e-commerce marketplace (10M+ DAUs). Product pages show a grid of images; each image must be uploaded, then may be served from cache (if already present), and finally rendered on the client. Poor scheduling increases tail latency and hurts conversion.
To model this, you are given a stream of image requests. Each request has an image_id, an upload_time (seconds needed if not cached), and a render_time (seconds needed on the client once the bytes are available). The system has:
C images (LRU eviction).If an image is already in cache at request time, its upload is skipped (0 seconds) and it becomes immediately eligible to render.
Implement schedule_images(requests, C) that returns the minimum possible time (in seconds) to finish rendering all requested images, assuming you can choose which eligible upload/render job to run next at any time.
requests: List[Tuple[int, int, int]] where each tuple is (image_id, upload_time, render_time) in the order requests arrive at time t=0 (all requests are known upfront).C: integer cache capacity.Example 1
requests = [(1, 5, 2), (2, 4, 3), (1, 5, 2)], C = 112Example 2
requests = [(1, 3, 10), (2, 3, 1)], C = 2141 <= len(requests) <= 2 * 10^50 <= upload_time, render_time <= 10^61 <= C <= 10^5image_id fits in 32-bit signed intThis is a scheduling + caching problem: you must account for LRU cache hits/misses and overlap of the upload/render pipelines to minimize the overall completion time.