collections — Counter, defaultdict & deque — Concept Check
Questions on collections — Counter, defaultdict & deque. Counter counts anything in one line, defaultdict removes key-existence boilerplate, deque gives O(1) queues — three imports that shorten half of all interview solutions.
Questions in this quiz
7 questions on collections — Counter, defaultdict & deque — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.You need to count the frequency of elements in a list for a majority element problem. Which collection should you use?
- ACounter
- Blist
- Cdeque
- Ddefaultdict
2.You want to implement a queue for a BFS algorithm. Which collection offers O(1) operations for both append and pop?
- Adefaultdict
- BCounter
- Clist
- Ddeque
3.You are grouping words by their sorted characters to find anagrams. Which collection simplifies this task?
- Aset
- Bdeque
- Cdefaultdict(list)
- DCounter
4.What happens if you access a missing key in a Counter object?
- ARaises an AttributeError
- BReturns None
- CRaises a KeyError
- DReturns 0
5.A developer uses list.pop(0) in a BFS implementation. What is the performance issue here?
- AO(log n) complexity for pop(0)
- BO(n) complexity for pop(0)
- CO(n^2) complexity for pop(0)
- DO(1) complexity for pop(0)
6.Which operation is NOT supported by the Counter class?
- Amost_common method
- BArithmetic operations
- CKeyError on missing keys
- DComparing two Counters
7.You need to maintain a fixed-size sliding window of data. Which collection should you use?
- Adefaultdict
- Blist with slicing
- CCounter
- Ddeque with maxlen