This section has 6 questions. For each question, read the code and type your answer under the Answer heading. You should answer the questions without running the code or consulting external resources to determine the output.
You have 15 minutes to complete this section.
Question A1
Assume you have a list of names, each entry has a first and last name. Write a list comprehension to generate the permutations of names, excluding the last names that are longer than 7 characters.
For example, names = ["John Smith", "Sally Anderson", "Ashwin Long", "Mandy Kim"], returns
["John Smith", "John Long", "John Kim", "Sally Smith", "Sally Long", "Sally Kim", "Ashwin Smith", "Ashwin Long", "Ashwin Kim", "Mandy Smith", "Mandy Long", "Mandy Kim"]
You answer should be a single line of code. Please assume that there is an existing variable called names containing a list of input names.
Question A2
What are the values of slice1, slice2, slice3, and slice4 at the end of this code block?
ar = [range(5, 30, 5)]
slice1 = ar[2:2]
slice2 = ar[:2]
slice3 = ar[::-1]
slice4 = ar[1:4:2]
Question A3
What error does the following code return and how do you fix it?
class Equity:
num_identifiers: int = 0
identifiers = []
def __init__(self, identifier, name):
self._identifier = identifier
self._name = name
Equity.track_identifier(identifier)
def track_identifier(cls, identifier):
cls.num_identifiers += 1
cls.identifiers.append(identifier)
eq1 = Equity("identifier1", "Apple")
eq2 = Equity("identifier2", "Google")
print("Total number of equities {Equity.num_identifiers}")
Question A4
What are the values of list1, list2, list3, and list4 at the end of this code block?
list1 = [(1, 2, 3, 4), (20, 40, 60), (100, 95, 90)]
list2 = list(list1)
list3 = list2
list1[1].append(80)
list1.append(5500)
list2[0] = (0, 1, 2, 3, 4, 5)
list3[1].remove(40)
list4 = list2.append(4200)
Question A5
What is printed when you run the code below?
def f():
x = []
def g(y=None):
if not x:
x.append(y)
elif y:
x.append(x[len(x)-1] + y)
return x
return g
h = f()
h(15)
h(25)
h(3)
print(h(2))
Question A6
Given the code below, what is returned for the best finance stock and best overall stock? Briefly explain your answer.
def add_tech_stocks(curr_stocks: dict) -> dict:
stocks = curr_stocks
stocks["APPL"] = 124.40
stocks["GOOG"] = 1569.15
stocks["AMZN"] = 3442.93
return stocks
def best_stock(stocks: dict) -> str:
curr_max = 0
curr_ticker = None
for key, value in stocks.items():
if value > curr_max:
curr_ticker = key
curr_max = value
return curr_ticker
finance_stocks = {"GS": 214.12, "MS": 10.61, "JPM": 102.44}
all_stocks = add_tech_stocks(finance_stocks)
best_finance = best_stock(finance_stocks)
best_overall = best_stock(all_stocks)
This set of Python interview questions focuses on slicing, list and object aliasing, closures, dictionary traversal, and class method usage. The key is to reason carefully about mutability and shared references, especially when shallow copies and in-place updates are involved. It also tests whether you can spot method-definition mistakes in a class and understand how to fix them correctly.