โ† All HACKERRANK Flashcard Decks

Python Data Structures & Algorithms 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 Python Data Structures & Algorithms flashcards as text
  1. What is the time complexity of inserting an element at the beginning of a Python list?

    Answer: O(n)

    Inserting at index 0 requires shifting all existing elements one position right, making it O(n).

  2. Which Python collection type uses a hash table internally and guarantees O(1) average-case lookup?

    Answer: dict

    Python dicts are hash maps, providing O(1) average-case get/set operations.

  3. What does `collections.deque` provide that a regular list does not?

    Answer: O(1) appends and pops from both ends

    deque is a doubly-linked list optimized for O(1) appendleft/popleft, unlike list which is O(n) at the left end.

  4. Given `d = defaultdict(list)`, what happens when you access `d['new_key']`?

    Answer: An empty list is created and returned

    defaultdict calls the factory function (list) to create a default value when a missing key is accessed.

  5. Which algorithm does Python's built-in `sort()` use?

    Answer: Timsort

    Python uses Timsort, a hybrid of merge sort and insertion sort, with O(n log n) worst case.

  6. What is the space complexity of a recursive Fibonacci function without memoization for input n?

    Answer: O(n)

    The call stack depth reaches n at most, so space complexity is O(n) even though the time complexity is O(2^n).

  7. Which data structure is most appropriate for implementing a LIFO (Last-In, First-Out) pattern in Python?

    Answer: list used with append/pop

    A Python list with append() to push and pop() to pull from the end naturally implements a LIFO stack.