Python · Module 3Module material
Data Structures interview questions
25 questions interviewers ask on this module’s topics, from first principles to the follow-ups. Each comes with a model answer.
Choosing a structure
- What is the difference between a list, a tuple, a set and a dictionary?●
- When would you choose a tuple over a list?●●
- Why is
x in some_setfaster thanx in some_list?●● - What does "hashable" mean, and which built-in types are hashable?●●
Lists
- What do
week[-1],week[-3:]andweek[::-1]give?● - What happens when you index past the end of a list, and when you slice past it?●
- What is the difference between
sort()andsorted()?●● - What is aliasing? How do you copy a list?●●
- What is the difference between
x = x + [4]andx += [4]for a list?●● - What does
[[0] * 3] * 3build, and why is it a trap?●● - Why should you not remove items from a list while looping over it?●●
Tuples
- How do you write a tuple with one item?●
- Can a tuple change?●●
- What is unpacking, and what happens when the counts do not match?●●
Sets
- How do you create an empty set?●
- What are union, intersection, difference and symmetric difference?●●
- What is the difference between a set's
removeanddiscard?●●
Dictionaries
- What is the difference between
d[key]andd.get(key)?● - Are dictionaries ordered?●●
- How do you count items in Python?●●
- What is a
defaultdict, and what is its trap?●● - How do you loop over a dictionary's keys and values together?●●
Strings
- Strings are immutable. What does that mean for their methods?●●
- How do you check whether a string is a palindrome?●●
Comprehensions
- What is a list comprehension, and when should you not use one?●●