HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

interview question, friends, movies and likes

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
movieslikesinterviewandquestionfriends

Problem

I was recently asked to implement an interface for a job interview. the class has methods to add customers and movies, the customers can watch or like movies and add friends. there are methods to get recommendations for users. All public methods in SocialMoviesImpl were defined by the interface, so I could not change them

the company decided not to continue with the hiring process, I would like some feedback in what I implemented. I used hashmaps to store the information since it is fast to access

```
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class SocialMoviesImpl implements SocialMovies {

Map movies;
Map customers;

public SocialMoviesImpl(){
this.movies= new HashMap();
this.customers= new HashMap();

}

/**
* add a movie, runtime complexitiy is O(1) //hashmap
* @param movieId the id of the movie
*
*/
public void addMovie(int movieId, String title) {
this.movies.put(movieId,title);

}
/**
* gets a movie, O(1)
*/
public String lookupMovie(int movieId) {
return this.movies.get(movieId);
}

/**
* adds a new customer, O(1)
*/
public void addCustomer(int customerId, String name) {
this.customers.put(customerId, new Customer(customerId, name));
}

/**
* obtains the customer, O(1)
*/
public String lookupCustomer(int customerId) {
Customer cust= this.customers.get(customerId);
return cust!=null?cust.getName():null;
}

/**
* O(4), 3 searches and 1 add
*/
public void addLikedMovie(int customerId, int movieId) {
if(this.customers.containsKey(customerId) && this.movies.containsKey(movieId)){
this.customers.get(customerId).addLike(movieId);
}else{
throw new IllegalArgumentException("movie or client does not exists");
}

}
/**
* O(4)
*/
public void addWatchedMovie(int customerId, int movieId) {
if(this.customers.containsKey(customerId) && this.movies.containsKey(movieId)){
this.customers.get(customerI

Solution

Your getRecommendationsFromFriends and getFriendRecommendations implementations are incorrect as the documentation shows:

  • getRecommendationsFromFriends: return movie id's



  • getFriendRecommendations: return customer id's



This is the biggest issue with your implementation.

getRecommendationsFromFriends is fairly straight-forward:

public Collection getRecommendationsFromFriends(int customerId) {
    /* 1. Validate Customer
     * 2. Collect the id's for movies his friends like
     * 3. Remove the customer's watched movies from the friend likes
     * 4. Return the collection (a set is good here, maybe even a TreeSet for sorting
     */
}


2 and 3 could be combined (a helper method in Customer could be handy)

GetFriendRecommendations is slightly more complicated, but not unreasonable for an interview. (keep in mind there could be many many customers!). Remember to always read (and re-read!) the documentation to make sure you have a firm grasp on what the interface is supposed to provide.

Code Snippets

public Collection<Integer> getRecommendationsFromFriends(int customerId) {
    /* 1. Validate Customer
     * 2. Collect the id's for movies his friends like
     * 3. Remove the customer's watched movies from the friend likes
     * 4. Return the collection (a set is good here, maybe even a TreeSet for sorting
     */
}

Context

StackExchange Code Review Q#21633, answer score: 4

Revisions (0)

No revisions yet.