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
What is the space complexity of a recursive Fibonacci function without memoization?
Answer: O(n)
The call stack depth reaches O(n) since each call adds one frame before returning.
Which technique avoids recomputing overlapping subproblems by storing results in a table?
Answer: Dynamic programming
Dynamic programming uses memoization or tabulation to store and reuse subproblem solutions.
What does `functools.lru_cache(maxsize=None)` do when applied to a recursive function?
Answer: Caches return values to avoid recomputation
`lru_cache` memoizes function call results so identical inputs return cached output instantly.
Given `s = 'racecar'`, which one-liner checks if it is a palindrome?
Answer: s == s[::-1]
`s[::-1]` slices the string in reverse; if it equals the original, the string is a palindrome.
What is the output of `sorted([('b', 2), ('a', 3), ('c', 1)], key=lambda x: x[1])`?
Answer: [('c', 1), ('b', 2), ('a', 3)]
Sorting by the second element (index 1) orders the tuples by their numeric values ascending.
A two-pointer approach is most effective for problems involving:
Answer: Sorted arrays or strings with pair/window conditions
Two pointers work efficiently on sorted data to find pairs, subarrays, or windows meeting a condition in O(n).
What does `any(x > 5 for x in [1, 3, 7, 2])` return?
Answer: True
`any()` returns True if at least one element satisfies the condition — here 7 > 5 is True.