โ† All HACKERRANK Flashcard Decks

Python Data Structures & Algorithms 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 Python Data Structures & Algorithms flashcards as text
  1. What is the purpose of the `__init__` method in a Python class?

    Answer: To initialize the attributes of a new object when it is created

    The __init__ method is the constructor that runs automatically when a new instance of a class is created, allowing you to set initial values for the object's attributes.

  2. What does the `lambda` keyword create in Python?

    Answer: An anonymous (unnamed) function defined in a single expression

    Lambda creates a small anonymous function that can take any number of arguments but can only contain a single expression. It is often used with functions like map(), filter(), and sorted().

  3. What is the output of: print(type({}))?

    Answer:

    Empty curly braces {} create an empty dictionary in Python, not an empty set. To create an empty set, you must use set(). This is a common Python gotcha.

  4. Which method would you use to remove and return the last element from a Python list?

    Answer: pop()

    The pop() method without arguments removes and returns the last element of a list. With an index argument, it removes and returns the element at that position.

  5. What is a Python generator and how does it differ from a regular function?

    Answer: A function that uses yield to produce a sequence of values lazily, one at a time

    Generators use the yield keyword to produce values one at a time on demand (lazy evaluation), saving memory compared to creating an entire list. They maintain state between yields.

  6. What does the `*args` parameter allow in a Python function definition?

    Answer: Accepting a variable number of positional arguments as a tuple

    *args collects any number of positional arguments into a tuple, allowing the function to be called with varying numbers of arguments. Similarly, **kwargs collects keyword arguments into a dictionary.