โ† All HACKERRANK Flashcard Decks

Collections and Data Structures Flashcards

7 cards from real HACKERRANK practice questions. Tap to flip, then mark Knew It or Still Learning โ€” missed cards come back until you master them.

Read the first 7 Collections and Data Structures flashcards as text
  1. What does `Counter.subtract()` do differently from subtracting two Counters with `-`?

    Answer: subtract() allows negative counts; - removes non-positive counts

    `subtract()` allows counts to go to zero or negative, while the `-` operator drops non-positive counts.

  2. What is the maximum length constraint enforced when creating a `collections.deque` with `maxlen=3` and you append a 4th element?

    Answer: The oldest element on the opposite end is discarded

    When maxlen is set and the deque is full, appending to one end automatically discards an element from the other end.

  3. Which `collections.OrderedDict` method moves an existing key to either end?

    Answer: move_to_end()

    `move_to_end(key, last=True)` moves an existing key to the right end (or left end if last=False).

  4. What does `namedtuple._fields` return for `Point = namedtuple('Point', 'x y')`?

    Answer: ('x', 'y')

    `_fields` is a tuple of strings listing the field names of the named tuple.

  5. How does `collections.ChainMap` handle key lookups when the same key exists in multiple maps?

    Answer: Returns the value from the first map where the key is found

    ChainMap searches through the maps in order and returns the first match found.

  6. What is the result of: `Counter([1,1,2,3]) + Counter([1,2,2])`?

    Answer: Counter({1: 3, 2: 3, 3: 1})

    Counter addition combines counts for each element: 1 appears 2+1=3 times, 2 appears 1+2=3 times, 3 appears 1 time.

  7. Which Python built-in can you use to convert a named tuple instance to a dictionary?

    Answer: point._asdict()

    `_asdict()` returns an OrderedDict (or regular dict in Python 3.8+) mapping field names to values.

Collections and Data Structures Flashcards โ€” HACKERRANK Study Cards with Answers