...

/

Feature #11: Generate Movie Viewing Orders

Feature #11: Generate Movie Viewing Orders

Implementing the "Generate Movie Viewing Orders" feature for our "Netflix" project.

Description

We want to offer marathons for our viewers. Each marathon will have a fixed set of movies catering to a specific taste. For a given marathon, different viewing orders of the movies will yield different user satisfaction results. We want to experiment (A/B testing) with different viewing orders of the same marathon.

Your task is to generate all the possible permutations of movies in a given marathon.

Let’s look at an example to better understand this:

Solution

To solve this problem, we will use the backtracking approach.

We will assume a backtrack function that takes the index of the first movie to consider as one of the arguments.

  • If the first movie to consider has index size, then that means that the current permutation is done.

  • We will iterate over the marathon from index first to index size - 1.

  • We will place the ith movie first in the permutation, that is, moviesList[first], moviesList[i] = moviesList[i], moviesList[first].

  • We will proceed to create all the permutations that start from the ith movie: backtrack(first + 1).

  • Now we will backtrack, that is, moviesList[first], moviesList[i] = moviesList[i], moviesList[first] back.

Let’s look at some illustrations to better understand this:

Note: We are using numbers to represent movies in the following illustration.

Let’s look at the code for this solution:

class Solution{
public static void backTrack(int first, int size, List<String> moviesList, List<List<String>> output) {
// If all strings of given array `moviesList` are used and
// and Backtracking is performed add the permutations to output array.
if (first == size) {
List<String> temp = new ArrayList<String>(moviesList);
output.add(temp);
}
// Perform Backtracking for the size of a given array.
for (int i = first; i < size; i++) {
// Swap: In the current permutation place i-th integer first.
Collections.swap(moviesList, first, i);
// Complete permutations using the next integers.
backTrack(first + 1, size, moviesList, output);
// Swap and Backtrack
Collections.swap(moviesList, first, i);
}
}
public static List<List<String>> generatePermutations(String[] movies) {
List<List<String>> output = new LinkedList<>();
int size = movies.length;
// convert movies into list since the output is a list of lists
ArrayList<String> moviesList = new ArrayList<String>();
for (String movie : movies)
moviesList.add(movie);
backTrack(0, size, moviesList, output);
return output;
}
public static void main(String[] args) {
// Example #1
String[] input = {"Frozen","Dune","Coco"};
List<List<String>> output = generatePermutations(input);
System.out.println("Output 1: " + output);
// Example #2
String[] input2 = {"Frozen","Dune","Coco","Melificient"};
output = generatePermutations(input2);
System.out.println("Output 2: " + output);
// Example #3
String[] input3 = {"Dune","Coco"};
output = generatePermutations(input3);
System.out.println("Output 3: " + output);
}
}
Generate movie viewing orders

Complexity measures

Time
...