← All HACKERRANK Flashcard Decks

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 Algorithms flashcards as text
  1. What is the output of `list(map(lambda x: x**2, [1, 2, 3, 4]))`?

    Answer: [1, 4, 9, 16]

    `map` applies the lambda squaring function to each element, producing [1, 4, 9, 16].

  2. Which traversal of a binary tree visits nodes in the order: left subtree, root, right subtree?

    Answer: In-order

    In-order traversal follows left → root → right, which visits BST nodes in sorted order.

  3. What Python built-in can find the greatest common divisor of two integers?

    Answer: math.gcd(a, b)

    `math.gcd(a, b)` computes the greatest common divisor using the Euclidean algorithm.

  4. A sliding window of size k moves across an array of size n. What is the total number of windows?

    Answer: n - k + 1

    The first window starts at index 0 and the last at index n-k, giving n-k+1 total windows.

  5. What does `heapq.nlargest(3, [5, 1, 8, 3, 9, 2])` return?

    Answer: [9, 8, 5]

    `heapq.nlargest(k, iterable)` returns the k largest elements in descending order.

  6. Which sorting algorithm has the best average-case and worst-case time complexity of O(n log n)?

    Answer: Merge Sort

    Merge sort always divides and merges in O(n log n) regardless of input, unlike quicksort which degrades to O(n²).

  7. In Python, what is the result of `[x for x in range(10) if x % 2 == 0]`?

    Answer: [0, 2, 4, 6, 8]

    The list comprehension filters range(10) for even numbers (remainder 0 when divided by 2).