Quizzescollections — Counter, defaultdict & deque — Concept Check
intermediate
python

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.

7 minutes 7 questions Pass at 70%
Time Limit
7 minutes - timer starts when you begin
Change Answers Freely
Revisit and change any answer before submitting
Full Review After Submit
Correct answers and explanations shown for every question
Auto-submit on timeout
Quiz submits automatically when time runs out

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. 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. 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. 3.You are grouping words by their sorted characters to find anagrams. Which collection simplifies this task?

    • Aset
    • Bdeque
    • Cdefaultdict(list)
    • DCounter
  4. 4.What happens if you access a missing key in a Counter object?

    • ARaises an AttributeError
    • BReturns None
    • CRaises a KeyError
    • DReturns 0
  5. 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. 6.Which operation is NOT supported by the Counter class?

    • Amost_common method
    • BArithmetic operations
    • CKeyError on missing keys
    • DComparing two Counters
  7. 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