Collections and Data Structures Flashcards
6 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 6 Collections and Data Structures flashcards as text
A developer is processing a large log file where each line contains a user's action. They need to count the occurrences of each unique action to identify the most common ones. Which Python collection is the most efficient and idiomatic for this task?
Answer: A `collections.Counter` object.
The `collections.Counter` is a specialized dictionary subclass designed specifically for counting hashable objects. It simplifies the process of tallying counts from an iterable, providing a more efficient and readable solution than manually implementing the logic with a standard dictionary.
What is the output of the following Python code snippet? ```python set_a = {1, 2, 3, 4, 5} set_b = {4, 5, 6, 7, 8} result = set_a.symmetric_difference(set_b) print(sorted(list(result))) ```
Answer: [1, 2, 3, 6, 7, 8]
The `symmetric_difference()` method returns a new set containing elements that are in either `set_a` or `set_b`, but not in both. The elements {1, 2, 3} are unique to `set_a`, and {6, 7, 8} are unique to `set_b`. The combination of these is {1, 2, 3, 6, 7, 8}. The code then converts this set to a list and sorts it.
You are creating a data structure to hold the configuration settings for an application. These settings are defined once at startup and should never be changed during the program's execution to prevent accidental modification. Which of the following data structures is the most appropriate choice?
Answer: A tuple, because it is immutable and ordered.
A tuple is the best choice because it is both immutable and ordered. Immutability prevents the settings from being changed after they are created, which is a key requirement. Its ordered nature ensures that the settings remain in a consistent sequence, which can be important for configuration.
A programmer is building an index to group a list of words by their first letter. If a letter has not been seen before, a new list should be created for it. Which of the following is the most suitable and efficient approach to initialize the index?
Answer: A `collections.defaultdict(list)`.
A `collections.defaultdict(list)` is ideal for this scenario. It automatically creates a new list (the default value) the first time a key is accessed that does not exist in the dictionary. This avoids the need for explicit checks to see if the key is already present, leading to more concise and readable code.
Which of the following statements about the `collections.deque` object in Python is true?
Answer: It provides O(1) time complexity for append and pop operations from both ends.
`collections.deque` is a double-ended queue implemented as a doubly-linked list. This structure allows for highly efficient additions and removals from both the left and right sides (the 'ends' of the queue) with constant time complexity, O(1). In contrast, a standard list has O(n) complexity for appends and pops from the left.
You need to store a collection of unique, hashable items where the order of elements does not matter, and you need to perform fast membership testing (i.e., checking if an item is in the collection). Which data structure should you use?
Answer: A `set`
A `set` is the ideal data structure for this use case. Sets are unordered collections of unique elements. They are implemented using hash tables, which makes membership testing (e.g., `item in my_set`) a very fast operation, with an average time complexity of O(1).