Algorithms Flashcards
What is the time complexity of binary search?
O(log n) for a sorted array, as it halves the search space in each step.
What is the difference between DFS and BFS?
Depth-First Search (DFS) explores as far as possible along a branch (uses stack/recursion). Breadth-First Search (BFS) explores level by level (uses queue). DFS: O(V+E), BFS: O(V+E).
What is quicksort, and what is its average time complexity?
Quicksort partitions an array around a pivot, recursively sorting subarrays. Average: O(n log n), worst: O(n²) with poor pivot choice.
What is dynamic programming?
A method to solve problems by breaking them into overlapping subproblems, storing results (memoization or tabulation) to avoid recomputation.
Example: Fibonacci in O(n).
What is the time complexity of accessing an element in an array vs. a linked list?
Array: O(1) via index. Linked list: O(n) due to traversal.