Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Optimizing Mobile Image Loading

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Optimizing Mobile Image Loading. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

The Ally Financial mobile app must load several images for a screen, such as account illustrations or promotional content. Given image priorities, sizes, cached images, and a maximum number of concurrent downloads, determine the order in which images finish loading when downloads are scheduled greedily.

An image with a higher priority must be selected before a lower-priority image whenever a download slot becomes available. If priorities are equal, select the smaller image first. If both priority and size are equal, select the lexicographically smaller id. Cached images require no download and must be excluded.

Formal Specification

Implement image_load_order(images, cached_ids, max_concurrent). images is a list of dictionaries containing a unique string id, a positive integer size_kb, and an integer priority. cached_ids is a list of image IDs already stored locally. max_concurrent is the positive number of simultaneous downloads. Return a list of image IDs in completion order. If multiple images finish at the same time, return them in lexicographic ID order.

Assume download duration is proportional to size_kb, so an image of 500 KB takes 500 time units. All uncached downloads are available at time zero.

Constraints

  • 1 <= len(images) <= 10^5
  • 1 <= max_concurrent <= len(images)
  • 1 <= size_kb <= 10^9
  • 0 <= priority <= 10^9
  • Image IDs are unique strings

Function Signature

def image_load_order(images, cached_ids, max_concurrent):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output