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 time complexity of binary search on a sorted list of n elements?
Answer: O(log n)
Binary search halves the search space each iteration, giving O(log n) time complexity.
Which Python built-in function returns the index of the leftmost value in a sorted list that is greater than or equal to a target, using the `bisect` module?
Answer: bisect.bisect_left
`bisect.bisect_left` returns the leftmost position where the target can be inserted to keep the list sorted.
Given a list `nums = [3, 1, 4, 1, 5, 9]`, what does `sorted(nums, reverse=True)` return?
Answer: [9, 5, 4, 3, 1, 1]
`sorted()` with `reverse=True` returns a new list sorted in descending order.
Which algorithm sorts by repeatedly finding the minimum element from the unsorted portion and placing it at the beginning?
Answer: Selection Sort
Selection sort scans the unsorted portion each pass to select the minimum and swap it into position.
What is the worst-case time complexity of quicksort?
Answer: O(n^2)
Quicksort degrades to O(n²) when the pivot is always the smallest or largest element (already sorted input with bad pivot choice).
In Python, what does `collections.Counter([1,1,2,3,3,3])` return?
Answer: Counter({3: 3, 1: 2, 2: 1})
`Counter` returns a dictionary-like object with elements as keys and their counts as values, ordered by most common.
Which data structure gives O(1) average time for insert, delete, and lookup?
Answer: Hash table (dict/set)
Hash tables use hashing to achieve O(1) average-case for the three core operations.