Feature #6: Products Frequently Viewed Together

Implementing the "Products Frequently Viewed Together" feature for our "Amazon" project.

Description

We want to improve the recommendation system of products at Amazon. For this purpose, the management has decided that if a group of products is searched or viewed back to back by users, these products are similar or related. So, if another user is browsing one of these products, we can recommend the other ones to them. To determine these similar products, we will look at the previous activities of all users and check if particular products are viewed back to back.

Your task is to create a module that is given a dataset of product IDs in the order they were viewed by the user. You will also be given a list of products that are likely similar. Your job is to find how many times these products occur together in the dataset. The order of their occurrence does not matter unless they are back to back without any other products in between.

For example, let’s say the dataset of product IDs you are given is:{3, 2, 1, 5, 2, 1, 2, 1, 3, 4}. The candidates for similar products are {1, 2, 3}. We can see that these products occur together twice in the dataset, first at indices 0 to 2 and then at 6 to 8. The function’s output should be the list of the occurrences’ starting indices, which in this example is {0, 6]}.

The following illustration shows how products are mapped to IDs:

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