Python Data Structures - Interview Q&A for Morgan Stanley (Credit Analyst Role)
Q: What is the difference between a list and a tuple in Python? When would you prefer a
tuple over a list in financial data?
Lists are mutable, tuples are immutable.
Use a tuple when data must remain constant (e.g., fixed rating bands or categories).
Example: risk_bands = ('Low', 'Medium', 'High')
Q: How does a set differ from a list? Give an example where you'd prefer a set in a risk
model.
Sets do not allow duplicates and are unordered.
Use sets for fast lookups and ensuring uniqueness.
Example: unique_customers = set(customer_ids).
Q: What are dictionaries in Python? Can keys be mutable? Why or why not?
Dictionaries store key-value pairs.
Keys must be immutable (e.g., strings, numbers, tuples).
Mutable keys (like lists) are not allowed because dictionary keys must be hashable.
Q: You have a list of credit scores, and some scores are duplicated. How would you remove
the duplicates using built-in structures?
Use a set: unique_scores = list(set(credit_scores))
Q: You receive a nested dictionary with customer financials. How would you access the
'income' of the second customer?
Assuming: data = {'cust1': {...}, 'cust2': {'income': 50000}}
Access: data['cust2']['income']
Q: Suppose you want to filter customers with credit score > 750 from a list of dictionaries.
How would you do that in Python?
filtered = [cust for cust in customers if cust['score'] > 750]
Q: Write a Python function that takes a list of customer balances and returns the average
balance.
def average_balance(balances):
return sum(balances) / len(balances) if balances else 0
Q: Given two lists: one with customer names and another with their risk scores, how do you
combine them into a dictionary?
names = ['Alice', 'Bob']
scores = [700, 680]
data = dict(zip(names, scores))
Q: How would you sort a dictionary of customers by their credit scores in descending order?
sorted_scores = sorted(scores.items(), key=lambda x: x[1], reverse=True)
Q: How would you count how many customers fall into each risk bucket: Low, Medium,
High?
from collections import Counter
bucket_counts = Counter(risk_buckets)
Q: You have credit limit values with some missing entries represented by None. How would
you clean this list using list comprehension?
cleaned = [x for x in credit_limits if x is not None]
Q: Explain the time complexity difference between using a list vs a set for membership tests
in Python.
List: O(n), Set: O(1) on average
Sets are more efficient for large datasets when checking if an item exists.
Q: In risk scoring, how can dictionaries of lists be used to organize exposures by portfolio
name?
Example:
portfolios = {
'Retail': [1000, 1200],
'Corporate': [3000, 2500]
Q: How would you handle large nested JSON data (customer data dump from an API) using
Python data structures?
Use the json module to parse data, then navigate using nested dict and list accesses.
import json
data = json.loads(api_response)
customer_income = data['customers'][0]['income']