A programming test with 15 medium-level questions and answers on data structures and algorithms.
1. What is 'dynamic programming' used for?
💡 Dynamic programming solves complex problems by breaking them into simpler, overlapping subproblems and storing results.
2. What is 'recursion' in programming?
💡 Recursion occurs when a function calls itself to solve smaller instances of the same problem.
3. What is 'Big O notation' used for?
💡 Big O notation describes how an algorithm's time or space requirements grow relative to input size.
4. What is a 'graph' in data structures?
💡 A graph is a data structure consisting of nodes (vertices) connected by edges.
5. What is the purpose of a 'hash function'?
💡 A hash function converts input data into a fixed-size output, typically used for indexing in hash tables.
6. What does 'hashing' typically provide in a hash table?
💡 Hashing provides fast average-case performance for lookup, insertion, and deletion operations in a hash table.
7. What is a 'stack' data structure?
💡 A stack follows the Last In, First Out (LIFO) principle, where the most recently added item is removed first.
8. What is the time complexity of accessing an element in an array by index?
💡 Accessing an array element by index takes constant time, O(1), since the memory address can be calculated directly.
9. What is a 'queue' data structure?
💡 A queue follows the First In, First Out (FIFO) principle, where the first item added is the first removed.
10. Which sorting algorithm repeatedly steps through a list, swapping adjacent elements if they are in the wrong order?
💡 Bubble sort repeatedly compares and swaps adjacent elements to gradually sort a list.
11. What is the time complexity of searching in a balanced binary search tree?
💡 Searching in a balanced binary search tree takes O(log n) time due to its balanced structure.
12. What is the worst-case time complexity of quicksort?
💡 Quicksort's worst-case time complexity is O(n²), which occurs with poor pivot selection.
13. What is a 'binary tree'?
💡 A binary tree is a hierarchical data structure where each node has at most two children.
14. What is a 'linked list'?
💡 A linked list is a data structure made of nodes, each pointing to the next node in the sequence.
15. Which sorting algorithm uses a divide-and-conquer approach with O(n log n) average time complexity?
💡 Merge sort divides the array into halves, sorts them recursively, and merges them, achieving O(n log n) average complexity.