Question presented to candidate:
"Beyond list, dict, set and tuple, Python ships a collections module. Which types in it do you actually use, what problem does each one solve, and when would you pick it over a plain dict or list?"
What a strong answer should cover:
Counter(counting,most_common, multiset arithmetic),defaultdict(a factory for missing keys),namedtuple(a tuple with field names),deque(O(1) at both ends,maxlen).OrderedDictstill matters after dicts became ordered:move_to_end,popitem(last=False)and order-sensitive equality, the classic LRU building block.ChainMap(layered lookups over several dicts without copying) andUserDict/UserList/UserString(safe bases for subclassing, because adictsubclass'supdate()skips your__setitem__).- Gotchas: reading a missing key creates it in a
defaultdict;Countersubtraction drops zero and negative counts;namedtupleis immutable. - A version fact: the ABC aliases such as
collections.Mappingwere removed in 3.10, so import them fromcollections.abc.
Code / implementation expected: Optional โ short snippets per type, ideally a word count with Counter and grouping with defaultdict(list).