โ† All HACKERRANK Flashcard Decks

Built-in Functions and Lambdas 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 Built-in Functions and Lambdas flashcards as text
  1. A list of dictionaries needs to be sorted based on the 'age' of each person. Which of the following code snippets correctly performs this sort?

    Answer: sorted(people, key=lambda person: person['age'])

    The built-in `sorted()` function returns a new sorted list. It takes a `key` argument, which should be a function that takes one element and returns a value to sort by. A lambda function `lambda person: person['age']` is the perfect tool for this, as it takes a dictionary (`person`) and returns the value associated with the 'age' key for comparison.

  2. What is the primary characteristic that distinguishes a lambda function from a standard function defined with `def`?

    Answer: A lambda function is restricted to a single expression and implicitly returns its result.

    Lambda functions are syntactically restricted to a single expression. The value of this expression is what the function implicitly returns. In contrast, functions defined with `def` can contain multiple statements, including explicit `return` statements, loops, and assignments.

  3. Given a list of integers, `numbers = [10, 15, 21, 33, 42, 55]`, which line of code will produce a new list containing only the numbers divisible by 5?

    Answer: list(filter(lambda x: x % 5 == 0, numbers))

    The `filter()` built-in function is used to construct an iterator from elements of an iterable for which a function returns true. The lambda function `lambda x: x % 5 == 0` correctly tests each number for divisibility by 5, and `filter()` includes only those that pass the test. `map()` would apply the function to every element, returning a list of booleans, not the filtered numbers.

  4. What will be the output of the following Python code snippet? ```python from functools import reduce items = result = reduce(lambda x, y: x * y, items) print(result) ```

    Answer: 24

    The `reduce` function from the `functools` module applies a function of two arguments cumulatively to the items of an iterable. In this case, `lambda x, y: x * y` multiplies the elements. The process is: 1*2=2, then 2*3=6, then 6*4=24. The final accumulated value is 24.

  5. A developer needs to create a new list where each number from an original list is tripled. Which of the following is the most idiomatic use of a built-in function with a lambda to achieve this?

    Answer: list(map(lambda x: x * 3, original_list))

    The `map()` function applies a given function to each item of an iterable and returns an iterator of the results. The lambda `lambda x: x * 3` is the function that triples a number. `map()` applies this to every element, creating the desired transformed list. `filter()` is for selection, not transformation.

  6. In which scenario is using a lambda function most appropriate over a standard named function (`def`)?

    Answer: When a simple, one-off function is needed as an argument for a higher-order function like `sorted()` or `map()`.

    Lambda functions are ideal for situations where a small, anonymous function is needed for a short period, typically as an argument to a higher-order function. Their conciseness is a benefit here. For reusable, complex, or well-documented functions, a standard `def` is the better choice.