CS FINAL
What is the algorithmic complexity of searching for a value in a binary search tree?
O(log n)
What is the time complexity of calculating the length of a linked list?
O(n)
What is the time complexity of searching for an element in an unsorted linked list?
O(n)
Can two keys have the same hash value in a hash table? If yes, what is this situation called?
Yes, two keys can have the same hash value in a hash table. This situation is called a collision.
Which operator can be overloaded to access nodes from a linked list using indexing?
[]
What are the primary methods of the Python Queue class for adding and removing elements?
put and get
When implementing a custom Queue class, which methods should be included in the public interface?
put(), get(), empty()
In a binary tree, how many children can a node have at most?
2
Which of the following is NOT a consideration when choosing a data structure?
Accessibility of stored data
What is the key property that distinguishes a binary search tree (BST) from a regular binary tree?
All nodes in the left subtree must have smaller values than the parent node, and all nodes in the right subtree must have greater values.
True or False: A node in a linked list contains both the data to be stored and a reference to the previous node.
False
True or False: A priority queue serves elements based on their position in the queue rather than their assigned priority.
False
True or False: Dictionaries are efficient for retrieving values when the key is known, but not when the value is known.
False
True or False: Dictionaries are ordered collections of key-value pairs.
False
True or False: The append() method adds a new node at the beginning of a linked list.
False
True or False: The head of a linked list is always the last element in the list.
False
True or False: The put() method in a Python Queue adds an element to the front of the queue.
False
True/False: A binary tree can have at most three children for each node.
False
True/False: In a binary search tree, the left child of a node must have a value greater than its parent.
False
True/False: The root of a tree can have multiple parents.
False
Describe the main difference between a binary tree and a binary search tree.
A binary tree is a tree structure where each node can have at most two children, while a binary search tree is a specific type of binary tree that follows a specific ordering rule: all nodes in the left subtree must have smaller values than the parent node, and all nodes in the right subtree must have greater values.
What happens when two keys hash to the same index in a hash table?
A collision occurs and needs to be resolved
What is a hash function, and why is it important in the context of dictionaries?
A hash function is a method used to convert keys into non-negative integer indices for hash table storage. It is important in dictionaries as it determines the location where a key-value pair is stored and aids in efficient retrieval of values based on known keys.
Explain the concept of a hash table and its role in dictionary implementation.
A hash table is a data structure used to store key-value pairs, where keys are hashed into non-negative integer indices. It provides fast retrieval of values based on keys by utilizing a hash function to compute the index where the key's value is stored.
Explain the concept of a leaf node in a tree.
A leaf node is a node in a tree that has no children. It is the endpoint of a branch and does not have any outgoing links.
What is a priority queue?
A queue that assigns priorities to elements and serves them accordingly
What is a hash table?
A table where keys are converted to indices
Provide an example of an application where a priority queue would be useful.
An example of an application where a priority queue would be useful is in CPU scheduling, where processes with higher priority need to be executed before processes with lower priority.
Which data structures allow for storing elements of different types in Python?
Both lists and tuples
How can you determine if a linked list is empty?
By checking if the head is None
How does the basic insert algorithm for a binary search tree ensure the correct placement of a new node?
By comparing the new value with the current node and traversing left or right until a leaf node is reached, then inserting the new node as a child of the leaf node.
How can you remove a node from a linked list?
By updating the reference of the previous node
How can you insert a node at a specific position in a linked list?
By updating the references of the adjacent nodes
Which approach is used for handling collisions in open hashing?
Chaining
Describe how collisions are handled in open hashing.
Collisions in open hashing occur when two or more keys hash to the same index in the hash table. To handle collisions, open hashing uses separate chaining, where each index in the hash table contains a linked list of (key, value) pairs that hashed to that index.
Explain the concept of contiguous memory and its advantage in accessing elements in a list or tuple.
Contiguous memory refers to storing elements in a list or tuple where all the elements are placed after each other in memory. This allows for fast access to locations within the list/tuple by using the starting memory location and the index to compute the memory locations.
Which attribute(s) does a node in a linked list typically have?
Data and reference to the next node
What attributes are typically stored in a node of a binary search tree?
Data, left child link, right child link.
Which type of data structure is specifically optimized for retrieving values when the key is known?
Dictionaries
When might having a linear data structure, such as a linked list, be advantageous over using a dictionary?
Having a linear data structure, such as a linked list, might be advantageous over using a dictionary when the order of elements is important or when elements need to be accessed sequentially rather than by keys. Additionally, a linear data structure may be preferred when there is a need for duplicate keys or multiple references to the same value.
What is a limitation of linked lists compared to arrays or lists?
Higher memory usage
Which operation is NOT typically supported by dictionaries?
Inserting elements at specific indices
What is the advantage of using contiguous memory for storing elements in a list or tuple?
It allows for fast access to locations within the list/tuple.
How does a dictionary retrieve a value when the key is known?
It computes the hash index and searches a linked list
How does a hash function contribute to dictionary implementation?
It converts keys into non-negative integer indices
How is the head of a linked list defined?
It is the first node in the list.
What is the purpose of a tail attribute in a linked list?
It keeps track of the last element in the list.
In Python, how does the language handle memory allocation for lists containing mixed types of elements?
It stores memory addresses in each location, pointing to the actual data.
In open hashing, what data structure is typically used to handle collisions?
Linked lists
Which data structures can be considered linear, having two ends or sides?
Linked lists and dictionaries
What data structure is commonly used for collision handling in open hashing?
Linked lists are commonly used for collision handling in open hashing. Each index in the hash table contains a linked list where (key, value) pairs that hashed to that index are stored.
Which operation is not typically supported by binary search trees?
Merging two binary search trees.
What is one limitation of linked lists compared to arrays or lists?
One limitation of linked lists compared to arrays or lists is that linked lists do not provide constant time access to elements like arrays do. In a linked list, you need to traverse the list starting from the head to access a specific element, which takes linear time.
Name one real-world example where binary search trees can be applied.
One real-world example where binary search trees can be applied is in representing a directory structure in a computer's file system. Each directory can be represented as a node, and the binary search tree can be used to efficiently search for a specific directory or file within the structure.
Discuss the advantages and disadvantages of open hashing compared to closed hashing.
Open hashing allows an unlimited number of keys to map to the same hash location, providing flexibility in handling collisions. However, it can lead to increased memory usage due to the additional linked lists and potentially slower search time if collisions are frequent.
How does open hashing differ from closed hashing in terms of collision handling?
Open hashing handles collisions by using separate chaining, where multiple keys that hash to the same index are stored in a linked list. In closed hashing, collision handling involves finding alternative locations in the hash table when a collision occurs.
How does Python handle memory management in the case of circular references?
Python automatically detects and resolves circular references.
How does Python handle memory management for variables that go out of scope?
Python keeps track of memory usage and releases it when variables are no longer referenced.
How does Python handle memory management for variables that go out of scope?
Python keeps track of memory usage and releases it when variables are no longer referenced. This is done through automatic garbage collection and reference counting.
Which data structure is appropriate for implementing a First-In-First-Out (FIFO) behavior?
Queue
What is the main advantage of using a linked list over an array?
Quick insertion and deletion
What are some real-world examples where binary search trees are commonly used?
Representing the structure of a file system.
What is the term used for the first node in a tree?
Root
Which data structure is most suitable for implementing a depth-first search algorithm?
Stack
Which data structure is suitable for simulating a Last-In-First-Out (LIFO) behavior?
Stack
What is a common application of a priority queue?
Storing and servicing messages received on a network. Syntax parsing in programming languages. CPU scheduling in operating systems.
What is the purpose of the clear() method in a Queue class?
The clear() method in a Queue class is used to remove all elements from the queue, essentially emptying the queue.
In a priority queue, how are elements served when they have the same priority?
The element that was enqueued first is served first.
Explain the purpose of the head attribute in a linked list.
The head attribute in a linked list is a reference to the first element or the starting point of the list.
What are the key attributes stored in a node of a binary search tree?
The key attributes stored in a node of a binary search tree are the node's data (value), a link to the left child node, and a link to the right child node. These attributes allow for the hierarchical organization and traversal of the tree.
What are the primary methods of the Python Queue class for adding and removing elements?
The primary methods of the Python Queue class for adding and removing elements are put() and get().
In Python, how is memory allocated for a variable storing a single value?
The program asks the computer for an available memory address.
Explain the purpose of a hash function in the context of dictionary implementation.
The purpose of a hash function in dictionary implementation is to convert keys into non-negative integer indices for efficient storage and retrieval. It ensures that each key consistently maps to the same index, enabling fast lookup of values based on known keys.
What is the difference between the put() method in a Queue and a LifoQueue in Python?
The put() method adds an element to the top in a Queue, while it adds to the front in a LifoQueue.
What is the purpose of a root node in a tree?
The root node serves as the starting point or the topmost node of a tree. It is the ancestor of all other nodes and provides the root-to-leaf path for traversing the tree.
What are the two main attributes typically found in a node of a linked list?
The two main attributes typically found in a node of a linked list are data (to store the actual information) and a reference to the next node.
What distinguishes one linear data structure from another?
The way items are added and removed
What is the main advantage of dictionaries over other linear data structures?
They have faster retrieval of values based on known keys
What is the purpose of a hash function in a dictionary?
To convert keys into non-negative integers
How can you determine if a linked list is empty?
To determine if a linked list is empty, you can check if the head is pointing to None. If it is, the list is empty.
What is the purpose of a node in a linked list?
To establish the connections between elements
How would you remove a node from a linked list?
To remove a node from a linked list, you need to update the references of the adjacent nodes. Specifically, you update the next reference of the previous node to point to the next node, bypassing the node you want to remove.
What is the purpose of the clear() method in a Queue class?
To remove all elements from the queue
What is the purpose of the clear() method in a custom Queue class?
To remove all elements from the queue
What is the role of a hash table in dictionary implementation?
To store the key-value pairs
Which data structure is made up of nodes and allows a node to connect to multiple other nodes?
Tree
True or False: A good hash function will result in each possible hash value having an equal number of possible keys that hash to it.
True
True or False: Closed hashing ensures that only one key can map to a hash location.
True
True or False: Collisions in hash tables occur when two keys hash to the same index.
True
True or False: Hash tables are commonly used for implementing dictionaries.
True
True or False: Hash tables in open hashing are often implemented using linked lists.
True
True or False: Hashing is a process of converting keys into non-negative integer indices.
True
True or False: In open hashing, collision resolution involves searching for the next available index in the hash table.
True
True or False: Linked lists are a suitable data structure when we need to store a large amount of data.
True
True or False: Memory management in Python involves automatic garbage collection and reference counting.
True
True or False: Open hashing allows an unlimited number of keys to map to the same hash location.
True
True or False: Python allows for mixed types in its lists, making them suitable for storing heterogeneous data.
True
True or False: Searching for an element in a linked list has a time complexity of O(n).
True
True or False: The primary advantage of using tuples in Python is their ability to allocate memory more efficiently compared to lists.
True
True/False: Binary search trees are efficient for searching elements in a sorted list.
True
True/False: Trees are commonly used in search, game logic, autocomplete tasks, and graphics.
True
How does Python allocate memory for tuples compared to lists?
Tuples are allocated contiguous memory, while lists are not.
What are the primary advantages of using tuples in Python?
Tuples have a smaller memory footprint compared to lists. Tuples are immutable, ensuring data integrity. Tuples allow for efficient memory allocation for mixed types.
How can you traverse a linked list to perform an operation on each node?
Using a while loop
How are dictionaries implemented internally to achieve fast retrieval based on keys?
Using hash tables
How does a dictionary retrieve a value when the key is known? Provide a brief overview of the process.
When the key is known, a dictionary retrieves a value by first computing the hash index of the key using the hash function. It then searches the linked list located at that index for the specific key, and upon finding the corresponding (key, value) pair, returns the associated value.
When might having a linear data structure be disadvantageous?
When the order of elements is important
Which of the following is NOT a typical use case for binary search trees?
combining two trees
Which method is used to retrieve both the priority and value from a Python PriorityQueue?
get()
Which method is used to check if a Python Queue or Stack is empty?
is_empty()
Which method is used to add a node at the beginning of a linked list?
prepend()
Which method is used to check if a specific element exists in a linked list?
search()