Data Structures & Algorithms Exam Guide
Data Structures & Algorithms Exam Guide
Inheritance in Python allows a class to inherit properties and behaviors from another class, promoting code reuse and hierarchical class structuring. Types include single inheritance (one superclass), multiple inheritance (multiple superclasses), and multilevel inheritance (chain of classes inherited from one another). It improves the modularity and maintainability of code by allowing existing implementations to be extended or modified rather than rewritten .
Quick sort and merge sort both use divide-and-conquer by recursively dividing the problem into smaller subproblems. Quick sort selects a pivot and partitions the array around it, which can lead to O(n^2) performance in the worst case but is typically faster in practice with O(n log n) average performance than other sorts. Merge sort divides the array into halves, recursively sorts, and then merges, providing a stable O(n log n) performance irrespective of the input state but generally requires more space .
The 'finally' block in Python's exception handling is executed regardless of whether an exception has occurred or not, ensuring that cleanup code such as releasing resources or closing files is executed. This block helps maintain program's stability and consistency by providing a guaranteed way to free resources .
A binary search algorithm requires the input list or array to be sorted. It operates by dividing the array into halves to reduce the search space. The divide-and-conquer classification stems from its method of recursively dividing the problem into smaller subproblems, solving each recursively, and combining solutions to solve the original problem .
A hash table maps keys to values using a hash function, which computes the index in an array where each key-value pair is stored. This allows for average-case constant time complexity, O(1), for lookup, insertion, and deletion operations, making it significantly more efficient than linear structures like arrays or linked lists for these operations .
Primary stack operations include 'push' (insertion of an element), 'pop' (removal of the top element), and 'peek' (viewing the top without removing it). These operations reflect the LIFO principle by ensuring that the last element added ('pushed') is the first one to be removed ('popped').
Indentation in Python is crucial as it defines the scope of loops, functions, and classes. Unlike languages like C++ or Java that use braces '{ }' to determine code blocks, Python relies solely on indentation levels to distinguish between different blocks of code. This enforces readability and a consistent structuring method for all Python programmers .
A postfix expression eliminates the need for parentheses as operators follow their operands, allowing expressions to be evaluated in one left-to-right pass using a stack structure, without the need for precedence rules. This property provides suitability for stack-based machines and compilers due to its unambiguous and efficient evaluation process .
Encapsulation bundles data with methods operating on the data, restricting direct access to some components (e.g., using private variables). Abstraction hides complex details to present only relevant information. Together, they enable clean interfaces and interactions. For example, classes hide internal complexity while providing a public API; a car object might offer drive() without exposing engine mechanics .
A tree data structure consists of nodes, each with potential children, forming a parent-child relationship hierarchy. Unlike more general graphs, trees have no cycles and a single root node. Its types include binary trees (e.g., binary search trees) where each node has at most two children, AVL trees which maintain balanced heights, and B-trees used in databases for balanced storage of information .