Feature #11: Ad Serving

Implement the "Ad Serving" feature for our "Amazon" project.

Description

A legacy ad service at the Amazon backend creates a list of ads that match a user’s profile and preferences. We have an ad-serving API, read4(), which a client can call to retrieve the next 4 ads in the list.

The legacy API was developed when users were still working on PCs with monitors that had relatively smaller screen sizes and lower resolutions. Showing any more or fewer than four ads wasn’t practical. However, now, many of our users are visiting websites through mobile devices with very small screens or PCs with much larger displays. As a result, sometimes, we also need to serve more than or fewer than four ads at a time.

The client determines the number of ads that can be displayed on a certain client’s screen. This may be more or less than four. Without modifying the backend, we want to implement a read(n) function that will return the next N ads (or fewer, if we reach the end of the ads list).

Request flow

For this task, we have to implement a read(n) function, in which we have to use the ad-serving read() API. The read() API will read 4 ads at a time, but the read(n) function will read n ads, depending on the screen size. Note that the read(n) function can be called multiple times.

Let’s take a closer look at how the read4() API and the read(n) function works.

read4() API: The read4() API internally maintains a list of ads from which it delivers ads to the user. The next ...

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.