Hard algorithm questions on complexity, graph theory and advanced data structures!
1. What is the Master Theorem used for?
💡 The Master Theorem provides a method to determine the time complexity of divide-and-conquer algorithms given their recurrence relation.
2. What is the time complexity of heap sort?
💡 Heap sort builds a max-heap in O(n) then extracts elements in O(log n) each, giving O(n log n) overall.
3. What is the Floyd-Warshall algorithm used for?
💡 Floyd-Warshall finds shortest paths between all pairs of vertices in a weighted graph, running in O(V³) time.
4. What is a "red-black tree"?
💡 A red-black tree is a self-balancing binary search tree with color rules that guarantee O(log n) time for insert, delete, and search.
5. What is the difference between a tree and a graph?
💡 A tree is a special type of graph that is connected and acyclic (no cycles). A graph can have cycles and disconnected components.
6. What is "memoization" vs "tabulation" in dynamic programming?
💡 Memoization (top-down) uses recursion with caching. Tabulation (bottom-up) builds the solution iteratively from smaller subproblems.
7. What is a "minimum spanning tree"?
💡 An MST connects all vertices of a weighted graph with the minimum possible total edge weight, with no cycles.
8. What is the time complexity of accessing an element in a hash table in the worst case?
💡 In the worst case (all keys hash to the same bucket), hash table lookup degrades to O(n) linear search.
9. What is a "segment tree"?
💡 A segment tree allows efficient range queries (sum, min, max) and point updates on arrays in O(log n) time.
10. What is the difference between Prim's and Kruskal's algorithm?
💡 Both find MSTs. Prim's grows the tree from a starting vertex. Kruskal's sorts all edges and adds them if they don't form a cycle.
11. What is "topological sorting"?
💡 Topological sort linearly orders vertices of a Directed Acyclic Graph (DAG) respecting edge directions — used in task scheduling.
12. What does NP-complete mean?
💡 NP-complete problems are the hardest in NP — if any one can be solved in polynomial time, all NP problems can be.
13. What is Dijkstra's algorithm used for?
💡 Dijkstra's algorithm finds the shortest path from a source node to all other nodes in a weighted graph with non-negative weights.
14. What is Bellman-Ford algorithm used for?
💡 Bellman-Ford finds shortest paths from a source, and unlike Dijkstra, handles graphs with negative weight edges.
15. What does "P vs NP" mean in computer science?
💡 P vs NP asks whether every problem whose solution can be verified in polynomial time (NP) can also be solved in polynomial time (P).
16. What is Knuth-Morris-Pratt (KMP) algorithm used for?
💡 KMP finds occurrences of a pattern in a text in O(n+m) time by using a failure function to avoid redundant comparisons.
17. What is a "skip list"?
💡 A skip list uses multiple layers of sorted linked lists with express lanes, achieving O(log n) average search time.
18. What is the time complexity of merge sort?
💡 Merge sort always divides the array in half and merges, giving O(n log n) in all cases (best, average, worst).
19. What is a "trie" data structure?
💡 A trie (prefix tree) stores strings character by character, sharing common prefixes — efficient for autocomplete and dictionary lookups.
20. What is "amortized analysis" in algorithms?
💡 Amortized analysis calculates the average time per operation over a sequence, smoothing out occasional expensive operations.