Interview Questions from "Fluent Python" Chapter 2: Sequences
Easy Questions
What are the two main categories of sequences based on mutability?
How do container sequences differ from flat sequences?
What is the key advantage of list comprehensions over
map
/filter
?How do you prevent list comprehensions from leaking variables (Python 2 vs. Python 3)?
What is the purpose of
collections.namedtuple
?How does tuple unpacking work in Python?
What does the
*
operator do in tuple unpacking (e.g.,a, *rest = [1, 2, 3]
)?Why do Python slices exclude the last item (e.g.,
my_list[0:3]
)?How do you reverse a sequence using slicing?
What is the difference between
list.sort()
andsorted()
?
Medium Questions
Explain how generator expressions save memory compared to list comprehensions.
How would you use a list comprehension to generate a Cartesian product?
When should you use
bisect
instead of thein
operator for membership tests?How does
bisect.insort
maintain a sorted sequence efficiently?Why might
array.array
be preferable tolist
for numerical data?What is the purpose of
memoryview
in handling large datasets?How does
deque.rotate()
work, and when would you use it?What happens when you assign to a slice (e.g.,
my_list[2:5] = [20, 30]
)?Why does
my_list = [[]] * 3
create a list with shared references?How does the
key
parameter insorted()
enable case-insensitive sorting?
Complex Questions
Explain the behavior of
a += b
for mutable vs. immutable sequences.Why does
t[2] += [50, 60]
raise aTypeError
but still modify a tuple’s mutable element?How does NumPy’s
ndarray
improve performance for numerical operations?Discuss the performance trade-offs of using
deque
vs.list
for FIFO/LIFO operations.How can
memoryview.cast()
manipulate binary data without copying bytes?When would you use
array.tofile()
instead ofpickle
for saving numerical data?Explain how the
key
parameter in sorting functions leverages stability (e.g., Timsort).How does
bisect
support efficient table lookups (e.g., converting scores to grades)?Why is
deque
thread-safe for append/pop operations?Compare the performance of
array.fromfile()
vs. reading floats from a text file.
These questions cover core sequence operations, performance optimizations, and practical applications from the chapter, suitable for evaluating a candidate's depth of understanding.