WorksheetsMastering Data Structures
Total questions: 21
Worksheet time: 11mins
What is the main difference between an array and a linked list?
Arrays consist of nodes linked by pointers, while linked lists do not.
Linked lists allow index-based access while arrays do not.
Arrays can grow dynamically while linked lists have fixed sizes.
The main difference is that arrays have fixed sizes and allow index-based access, while linked lists are dynamic and consist of nodes linked by pointers.
How do you insert an element at the beginning of a linked list?
Remove the last element and add the new one at the end.
Create a new node, set its next to the current head, and update the head to the new node.
Insert the new node in the middle of the list.
Replace the current head with the new node without linking.
What is the time complexity of accessing an element in an array?
O(n^2)
O(1)
O(n)
O(log n)
Explain the concept of a stack and its main operations.
A stack is a LIFO data structure with main operations: push (add), pop (remove), and peek (view top).
A stack is a FIFO data structure with operations: enqueue (add), dequeue (remove), and front (view front).
A stack allows random access to elements and has operations: insert (add), delete (remove), and top (view top).
A stack is a linear data structure with operations: add (push), remove (pop), and size (get size).
What is the difference between a stack and a queue?
A stack can only hold integers, while a queue can hold any data type.
A stack uses LIFO, while a queue uses FIFO.
Both stack and queue use LIFO.
A stack uses FIFO, while a queue uses LIFO.
How do you implement a queue using two stacks?
Use two stacks: one for enqueueing and one for dequeueing. Transfer elements from the first stack to the second when needed.
Use three stacks to manage the queue operations.
Use a single stack for both enqueueing and dequeueing.
Implement a queue using an array only.
What is a binary tree and how does it differ from a binary search tree?
A binary tree is a type of graph; a binary search tree is a linear structure.
A binary tree is always balanced; a binary search tree can be unbalanced.
A binary tree is a tree structure with nodes having at most two children; a binary search tree is a binary tree with ordered nodes.
A binary tree can have any number of children; a binary search tree can have only one.
What traversal methods can be used on trees?
Level-order Traversal
Random Access Traversal
Circular Traversal
Depth-First Search (DFS) and Breadth-First Search (BFS) including Pre-order, In-order, Post-order for DFS.
Define a graph and its components.
A graph is defined by its components: lines and angles.
A graph is a collection of colors and shapes.
A graph is defined by its components: vertices (nodes) and edges (connections between nodes).
A graph consists of only edges without any nodes.
What is the difference between directed and undirected graphs?
Directed graphs have directed edges; undirected graphs have undirected edges.
Directed graphs can have cycles; undirected graphs cannot.
Directed graphs are used for trees; undirected graphs are used for networks.
Directed graphs are always weighted; undirected graphs are not.
How do you perform a depth-first search (DFS) on a graph?
Perform a breadth-first search instead of a depth-first search.
Start at a node, mark it as visited, explore each adjacent unvisited node recursively until all nodes are visited.
Only explore nodes that are directly connected to the starting node.
Visit all nodes in a random order without marking them as visited.
What is the purpose of a priority queue and how is it implemented?
To store elements in a sorted order for quick retrieval.
To manage elements based on their insertion time rather than priority.
The purpose of a priority queue is to manage a collection of elements with priorities, enabling efficient access to the highest priority element. It is commonly implemented using heaps.
To implement a stack data structure for last-in-first-out access.
What is the time complexity of inserting an element at the end of a linked list?
O(n^2)
O(n)
O(1)
O(log n)
What are the main characteristics of a binary search tree?
Each node has at most two children, and the left child is less than the parent while the right child is greater.
All nodes have exactly two children.
Nodes are arranged in a circular manner.
It is a type of graph with no cycles.
How do you perform a breadth-first search (BFS) on a graph?
Start at a node, explore all its neighbors before moving to the next level of nodes.
Only explore nodes that are directly connected to the starting node.
Visit nodes in a random order.
Perform a depth-first search instead of a breadth-first search.
What is the purpose of a hash table and how does it handle collisions?
To store data in a sorted manner; collisions are handled by sorting the data.
Collisions in hash tables are ignored and do not affect data retrieval.
A hash table stores key-value pairs and handles collisions using methods like chaining or open addressing.
Hash tables are used for storing large datasets without any specific structure.
What are the advantages of using a doubly linked list over a singly linked list?
A doubly linked list allows traversal in both directions, while a singly linked list only allows one-way traversal.
A doubly linked list uses less memory than a singly linked list.
Operations in a doubly linked list are always faster than in a singly linked list.
A doubly linked list can only store integers, while a singly linked list can store any data type.
What is a graph traversal algorithm and what are its common types?
Traversal algorithms are only applicable to trees, not graphs.
Graph traversal algorithms are used to find the shortest path between two nodes only.
Graph traversal algorithms are used to sort the nodes of a graph.
A graph traversal algorithm is a method for visiting all the nodes in a graph; common types include Depth-First Search (DFS) and Breadth-First Search (BFS).
What is the main advantage of using a hash table over an array?
Hash tables allow for faster data retrieval on average due to their O(1) access time.
Hash tables can only store integers, while arrays can store any data type.
Hash tables require less memory than arrays.
Hash tables are always sorted, while arrays are not.
What is the difference between a binary tree and a binary heap?
Binary heaps are used for searching, while binary trees are used for sorting.
A binary tree is a hierarchical structure, while a binary heap is a complete binary tree used for priority queues.
Binary heaps are always balanced, while binary trees can be unbalanced.
A binary tree can have any number of children, while a binary heap can only have two.
What is the time complexity of searching for an element in a balanced binary search tree?
O(log n)
O(n log n)
O(1)
O(n)
