blog-cover-image

Data Scientist Interview Questions from Soundcloud and American Express

In this article, we will walk through actual data scientist interview questions from Soundcloud and American Express, providing detailed solutions, code examples, and explanations of the underlying concepts. Whether you’re preparing for your next interview or simply interested in applied data science, this guide offers valuable insights and hands-on strategies.

Data Scientist Interview Questions from Soundcloud and American Express


1. Soundcloud Data Scientist Interview: Playlist Recommendation Problem

Question

For a user X, recommend 5 playlists they would like that they haven’t heard before. You can attempt this via any programming language of your choice. Explain your logic.

Concepts Involved

  • Collaborative Filtering
  • Content-Based Filtering
  • Similarity Measures (Cosine, Jaccard, etc.)
  • Data Preprocessing
  • Recommendation Evaluation

Solution Approach

In a real-world scenario like Soundcloud, users interact with playlists in various ways: listening, liking, saving, etc. The two most popular approaches for recommendation systems are:

  • Collaborative Filtering: Recommends playlists based on user-user or item-item similarities.
  • Content-Based Filtering: Recommends playlists similar to those the user has liked in the past, based on playlist metadata/features (e.g., genre, tags, artists).

For Soundcloud, collaborative filtering is often more effective, especially with implicit data (like listening history). Below, we'll solve this using Python and collaborative filtering (user-based).

Step-by-Step Solution

1. Data Representation

Assume we have the following data:

  • users: list of user IDs.
  • playlists: list of playlist IDs.
  • user_playlist_interactions: dictionary mapping each user ID to a set of playlist IDs they have listened to.

users = ['U1', 'U2', 'U3', 'U4', 'U5']
playlists = ['P1', 'P2', 'P3', 'P4', 'P5', 'P6', 'P7', 'P8']
user_playlist_interactions = {
    'U1': {'P1', 'P2', 'P3'},
    'U2': {'P2', 'P3', 'P4'},
    'U3': {'P1', 'P4', 'P5'},
    'U4': {'P2', 'P5', 'P6'},
    'U5': {'P3', 'P6', 'P7'},
}

2. Find Playlists User X Hasn't Heard


user_x = 'U1'
heard_playlists = user_playlist_interactions[user_x]
all_playlists = set(playlists)
unheard_playlists = all_playlists - heard_playlists
print(unheard_playlists)
# Output: {'P4', 'P5', 'P6', 'P7', 'P8'}

3. Compute Similarity Between Users

We use the Jaccard similarity (size of the intersection divided by the union) between sets of listened playlists:


def jaccard_similarity(set1, set2):
    intersection = len(set1 & set2)
    union = len(set1 | set2)
    return intersection / union if union != 0 else 0

similarities = {}
for user in users:
    if user == user_x:
        continue
    sim = jaccard_similarity(heard_playlists, user_playlist_interactions[user])
    similarities[user] = sim

print(similarities)
# Output: {'U2': 0.5, 'U3': 0.2, 'U4': 0.2, 'U5': 0.2}

4. Aggregate Recommendations from Similar Users

Find playlists that similar users have listened to, but user X hasn’t, and rank them by sum of similarities.


from collections import defaultdict

playlist_scores = defaultdict(float)
for user, sim in similarities.items():
    for pl in user_playlist_interactions[user]:
        if pl not in heard_playlists:
            playlist_scores[pl] += sim

# Sort by score descending
recommended_playlists = sorted(playlist_scores.items(), key=lambda x: x[1], reverse=True)

# Get top 5
top_5 = [pl for pl, score in recommended_playlists[:5]]
print(top_5)

Suppose the output is:


['P4', 'P5', 'P6', 'P7']

If less than 5, fill randomly or by global popularity.

5. Full Implementation Example


def recommend_playlists(user_x, user_playlist_interactions, playlists, top_n=5):
    heard_playlists = user_playlist_interactions[user_x]
    all_playlists = set(playlists)
    unheard_playlists = all_playlists - heard_playlists

    # Compute similarities
    def jaccard_similarity(set1, set2):
        intersection = len(set1 & set2)
        union = len(set1 | set2)
        return intersection / union if union != 0 else 0

    similarities = {}
    for user in user_playlist_interactions:
        if user == user_x:
            continue
        sim = jaccard_similarity(heard_playlists, user_playlist_interactions[user])
        similarities[user] = sim

    # Score playlists
    from collections import defaultdict
    playlist_scores = defaultdict(float)
    for user, sim in similarities.items():
        for pl in user_playlist_interactions[user]:
            if pl in unheard_playlists:
                playlist_scores[pl] += sim

    # Recommend top N
    recommended_playlists = sorted(playlist_scores.items(), key=lambda x: x[1], reverse=True)
    top_recommendations = [pl for pl, score in recommended_playlists[:top_n]]

    # Fill with random or popular if needed
    if len(top_recommendations) < top_n:
        remaining = list(unheard_playlists - set(top_recommendations))
        top_recommendations += remaining[:(top_n - len(top_recommendations))]

    return top_recommendations

# Example usage
recommend_playlists('U1', user_playlist_interactions, playlists)

6. Explanation

  • For user X, we look at similar users based on overlap in playlist history.
  • We score candidate playlists by summing similarities of users who have listened to them.
  • Top scoring playlists are recommended, ensuring user X hasn’t already listened to them.

This is a simplified version of user-based collaborative filtering. For large-scale systems, you would use matrix factorization or deep learning models (e.g., neural collaborative filtering).

7. Variations and Improvements

  • Weight recent interactions higher.
  • Incorporate playlist metadata (genre, mood, etc.).
  • Apply item-based collaborative filtering for scalability.
  • Hybrid approach: blend collaborative and content-based scores.

2. American Express Data Scientist Interview: First Round Questions

2.1 Dataframe Manipulations

Dataframe manipulations are foundational for any data scientist. Typical questions test your ability to filter, group, and transform data.

Example Question

Given the following DataFrame df:


import pandas as pd
data = {
    'user_id': [1, 2, 3, 4, 5],
    'purchase': [100, 200, 150, 120, 180],
    'country': ['US', 'IN', 'US', 'FR', 'IN']
}
df = pd.DataFrame(data)
  • Filter rows where country == 'US'
  • Calculate mean purchase per country

# Filter rows
us_df = df[df['country'] == 'US']

# Mean purchase per country
mean_purchase = df.groupby('country')['purchase'].mean()

These operations use basic pandas indexing and groupby.

2.2 Regex Pattern: Match a Phrase with up to 4 Random Words in Between

Question

Write a regex to find the phrase "high ... calorie" where up to 4 random words can occur between "high" and "calorie".

Concepts

  • Regex basics: word, whitespace, quantifiers
  • Non-capturing groups
  • Word boundaries

Solution

In regex, \b is a word boundary, \w+ matches a word, and \s+ matches whitespace. To match up to 4 words between "high" and "calorie":


import re

pattern = r'\bhigh(?:\s+\w+){0,4}\s+calorie\b'

text1 = "This is a high calorie food."
text2 = "This is a high fat calorie food."
text3 = "This is a high very saturated fat calorie food."

matches1 = re.findall(pattern, text1)
matches2 = re.findall(pattern, text2)
matches3 = re.findall(pattern, text3)
print(matches1, matches2, matches3)

This pattern matches:

  • high calorie
  • high fat calorie
  • high very saturated fat calorie

but not "high extremely super duper saturated fat calorie" (which is 5+ words between).

Explanation

  • high: literal word
  • (?:\s+\w+){0,4}: up to 4 occurrences of (space + word)
  • \s+calorie: space, then "calorie"

2.3 Preprocessing: Unidecode Errors, Punctuations, and Numbers

Text preprocessing is crucial before regex or NLP tasks.

Common Steps

  • Handle Unicode (e.g., accented characters)
  • Remove punctuations
  • Remove or standardize numbers

from unidecode import unidecode
import re

text = "Café123! High-fat-calorie foods are bad."

# Unidecode
text = unidecode(text)  # "Cafe123! High-fat-calorie foods are bad."

# Remove punctuations
text = re.sub(r'[^\w\s]', '', text)  # "Cafe123 Highfatcalorie foods are bad"

# Remove numbers
text = re.sub(r'\d+', '', text)      # "Cafe Highfatcalorie foods are bad"

This sequence ensures the string is ASCII, punctuation-free, and number-free.

2.4 Probability Question (Descriptive)

Probability questions often assess your understanding of basic concepts such as conditional probability, expectation, variance, and combinatorics.

Example Probability Question

Suppose you have a deck with 4 red and 6 black cards. If you draw 2 cards at random, what is the probability both are red?

Solution

Let’s denote:

  • Total cards = 10
  • Number of ways to choose 2 red cards: \( C(4,2) = 6 \)
  • Total ways to choose any 2 cards: \( C(10,2) = 45 \)

 

So, the probability is:

\[ P(\text{both red}) = \frac{C(4,2)}{C(10,2)} = \frac{6}{45} = \frac{2}{15} \]

Such questions test your grasp of combinations and basic probability.

2.5 Print Primes Between 0 and 100 (Coding)

A classic coding problem to test loops, conditionals, and basic math.


def is_prime(n):
    if n < 2:
        return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0:
            return False
    return True

for num in range(0, 101):
    if is_prime(num):
        print(num, end=' ')

This prints all prime numbers between 0 and 100. The primality check is efficient up to 100, as it only checks up to the square root of n.


Conclusion

Data scientist interviews at companies like Soundcloud and American Express demand a robust understanding of data manipulation, pattern recognition, probability, and algorithmic thinking. The playlist recommendation problem showcases the importance of collaborative filtering and similarity measures, while American Express’s questions reinforce the need for strong data handling, regex proficiency, and mathematical reasoning. Mastering these concepts and practicing such questions will significantly improve your chances of success in data science interviews.

Further Reading

Related Articles