Intro to Data Structures
- 51Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What is a data structure?
51 questions
- 1
What is a data structure?
Model answer
A data structure is a way to store and organize data in a computer so that the data can be used efficiently.
Explanation
The formal definition is given in the lesson: data structure is a way to store and organize data in a computer for efficient use.
- 2
What are the two ways to study data structures?
Model answer
They are studied as mathematical/logical models (abstract data types) and as concrete implementations.
Explanation
The lesson states we can talk about data structures in two ways: as mathematical/logical models (abstract view) and as implementations (concrete types).
- 3
Define an abstract data type (ADT).
Model answer
An ADT is a definition of data and operations without implementation details.
Explanation
The lesson defines ADTs as entities that are definitions of data and operation but do not have implementations.
- 4
Give an example of an ADT from the lesson.
Model answer
A list that stores a group of elements of a particular data type and allows reading and modifying elements by position.
Explanation
The lesson gives the example of a list ADT: store elements of a data type, read by position, modify by position.
- 5
What is the concrete implementation of a static list ADT?
Model answer
An array.
Explanation
The lesson says arrays give us all the functionalities of the static list ADT (store elements, read/modify by position).
- 6
Describe the operations of a dynamic list ADT as given in the lesson.
Model answer
It supports: checking if empty, inserting an element at any position, removing an element, counting the number of elements, reading/modifying element at a position, and specifying the data type.
Explanation
The lesson lists these operations for a dynamic list ADT.
- 7
How can a dynamic list be implemented using arrays?
Model answer
By declaring a large array and using a variable to mark the end of the list. Insertions may require shifting elements. When the array is full, a new larger array (typically double size) is created and elements are copied.
Explanation
The lesson explains the array-based implementation of a dynamic list with variable 'end' and handling overflow by doubling the array size.
- 8
What is the time complexity for accessing an element by index in an array-based list?
Model answer
Constant time, O(1).
Explanation
The lesson states that accessing an element in an array takes constant time because the address can be calculated from the base address and index.
- 9
What is the time complexity for inserting an element at a given position in an array-based list (worst case)?
Model answer
O(n), where n is the number of elements in the list.
Explanation
The lesson explains that inserting at a particular position requires shifting elements, and in the worst case (insertion at beginning), all elements must be shifted, so time is proportional to the list size.
- 10
What is the main disadvantage of using an array to implement a dynamic list?
Model answer
Memory inefficiency: some array space may be unused, and when the array is full, creating a new array and copying elements is costly.
Explanation
The lesson points out that with arrays, we may have unused memory and the cost of resizing when the array is full.
- 11
What is a linked list?
Model answer
A linked list is a data structure where data is stored in nodes, each node contains data and a pointer to the next node, and nodes are not necessarily contiguous in memory.
Explanation
The lesson introduces linked list as a collection of nodes with data and a link to the next node, stored in non-contiguous memory.
- 12
What is the head of a linked list?
Model answer
The first node in the linked list, identified by a pointer variable that stores its address.
Explanation
The lesson defines head as the first node, and the address of the head is the identity of the linked list.
- 13
What is the time complexity to access an element in a linked list (by position)?
Model answer
O(n), where n is the number of nodes.
Explanation
The lesson explains that to access an element, we must traverse from the head, so time is proportional to the list size.
- 14
How does the memory requirement of a linked list compare to an array for storing a list?
Model answer
Linked lists use extra memory for pointer variables but only allocate memory as needed, avoiding unused space. Arrays may have unused slots if allocated larger than needed.
Explanation
The lesson compares memory: arrays may have unused slots, linked lists allocate per node but use extra memory for pointers.
- 15
What is the time complexity for inserting a node at the beginning of a linked list?
Model answer
O(1) (constant time).
Explanation
The lesson states that inserting at the beginning requires only creating a new node and adjusting the head pointer, which is constant time.
- 16
What is the time complexity for inserting a node at the end of a singly linked list?
Model answer
O(n) in the worst case, because we must traverse to the end.
Explanation
The lesson explains that to insert at the end, we need to traverse the whole list to find the last node.
- 17
What are the advantages of a doubly linked list over a singly linked list?
Model answer
It allows traversal in both directions (forward and backward) and simplifies some operations like deletion because we can access the previous node directly.
Explanation
The lesson mentions that with a pointer to any node, we can access both next and previous, making reverse lookup and certain operations easier.
- 18
What is the disadvantage of a doubly linked list compared to a singly linked list?
Model answer
It uses extra memory for the additional pointer to the previous node, and more care is needed when resetting links during insertions/deletions.
Explanation
The lesson states that doubly linked lists use extra memory for the previous pointer and are more prone to errors due to more link updates.
- 19
Define a stack as an ADT.
Model answer
A stack is a list or collection with the restriction that insertion and deletion can be performed only from one end, called the top. It is a Last-In-First-Out (LIFO) structure.
Explanation
The lesson defines stack ADT as a collection with insertion (push) and deletion (pop) from the top, LIFO.
- 20
Name the two fundamental operations on a stack and their time complexity.
Model answer
Push (insert) and pop (delete), both take constant time O(1).
Explanation
The lesson states that push and pop are constant time operations.
- 21
List two applications of stack data structure.
Model answer
Execution of function calls (recursion) and implementing undo operations in editors.
Explanation
The lesson mentions stack used for function call execution and undo operations.
- 22
How can a stack be implemented using an array?
Model answer
By using an array and a variable 'top' that indexes the top element. Push increments top and writes, pop decrements top. Overflow can be handled by dynamic array (doubling size).
Explanation
The lesson describes array-based stack with top variable and dynamic resizing.
- 23
How can a stack be implemented using a linked list?
Model answer
By inserting and deleting nodes at the head of the linked list. The head pointer acts as the top. Push creates a new node and makes it the head; pop removes the head node.
Explanation
The lesson explains that inserting at the beginning of a linked list is constant time, so head serves as top for stack operations.
- 24
Define a queue as an ADT.
Model answer
A queue is a list or collection with the restriction that insertion (enqueue) is performed at one end (rear) and deletion (dequeue) at the other end (front). It is a First-In-First-Out (FIFO) structure.
Explanation
The lesson defines queue ADT as FIFO, with enqueue at rear and dequeue at front.
- 25
Name the two fundamental operations on a queue and their time complexity.
Model answer
Enqueue (insert at rear) and dequeue (remove from front), both should be O(1).
Explanation
The lesson states that all queue operations must take constant time.
- 26
What is a circular array and why is it used in array-based queue implementation?
Model answer
A circular array treats the array as a circle; when the end is reached, the next index is the beginning (using modulo arithmetic). It is used to reuse space that becomes available after dequeue operations.
Explanation
The lesson explains that without circular array, after many dequeues, the front moves forward and the space before front is wasted. Circular array allows wrapping around to reuse that space.
- 27
How can a queue be implemented using a linked list to ensure constant time for both enqueue and dequeue?
Model answer
By maintaining two pointers: 'front' for dequeue (removing from head) and 'rear' for enqueue (inserting at tail). Enqueue creates a new node and links it after the current rear; dequeue removes the node at front.
Explanation
The lesson explains that with both front and rear pointers, both operations are O(1).
- 28
Define a tree data structure.
Model answer
A tree is a non-linear hierarchical data structure consisting of nodes connected by edges, with a root node at the top and branches downward. Each node can have children, and there are no cycles.
Explanation
The lesson introduces tree as a hierarchical structure with root and children, used to represent hierarchical data.
- 29
What is a binary tree?
Model answer
A binary tree is a tree in which each node can have at most two children, typically called left child and right child.
Explanation
The lesson defines binary tree as having at most two children per node.
- 30
Define height of a tree.
Model answer
Height is the number of edges in the longest path from the root to a leaf node. Height of an empty tree is -1.
Explanation
The lesson defines height as the number of edges in the longest root-to-leaf path.
- 31
What is a complete binary tree?
Model answer
A binary tree where all levels except possibly the last are completely filled, and nodes in the last level are as far left as possible.
Explanation
The lesson defines complete binary tree with all levels full except possibly the last, which is left-filled.
- 32
What is the maximum number of nodes in a binary tree of height h?
Model answer
2^(h+1) - 1 (a perfect binary tree).
Explanation
The lesson calculates the maximum nodes as the sum of geometric series from level 0 to h: 2^(h+1)-1.
- 33
Define a binary search tree (BST).
Model answer
A BST is a binary tree where for each node, all values in the left subtree are less (or equal) and all values in the right subtree are greater. This property must hold for all nodes.
Explanation
The lesson defines BST with the property of left subtree ≤ node < right subtree (or strict).
- 34
What is the average-case time complexity for search, insertion, and deletion in a BST?
Model answer
O(log n) when the tree is balanced.
Explanation
The lesson states that in average case, if tree is balanced, operations are O(log n).
- 35
What is the worst-case time complexity for BST operations?
Model answer
O(n), which occurs when the tree is skewed (like a linked list).
Explanation
The lesson mentions worst case O(n) for an unbalanced tree.
- 36
How do you insert a node in a BST?
Model answer
Start at the root; if the value to insert is less than current node's value, go left; if greater, go right. When a null child is reached, insert the new node there.
Explanation
The lesson describes the recursive/iterative process of insertion in BST.
- 37
What are the three cases for deleting a node from a BST?
Model answer
- Node is a leaf: simply remove it. 2) Node has one child: replace the node with its child. 3) Node has two children: replace the node's value with the minimum value in its right subtree (or maximum in left), then delete that duplicate node.
Explanation
The lesson explains the three deletion cases with an example.
- 38
What is the in-order traversal of a BST?
Model answer
A traversal that visits left subtree, then root, then right subtree. For a BST, it yields the elements in sorted order.
Explanation
The lesson defines in-order traversal and notes that it gives sorted order for BST.
- 39
What is the time complexity of in-order traversal?
Model answer
O(n), where n is the number of nodes.
Explanation
The lesson states that each node is visited once, so time is proportional to number of nodes.
- 40
What is level-order traversal of a binary tree?
Model answer
A traversal that visits nodes level by level from left to right, starting from the root.
Explanation
The lesson defines level-order traversal as breadth-first, visiting all nodes at current depth before deeper levels.
- 41
What data structure is used to implement level-order traversal?
Model answer
A queue.
Explanation
The lesson explains that a queue is used to store discovered nodes and ensure FIFO order.
- 42
Define a graph as a mathematical structure.
Model answer
A graph G is an ordered pair (V, E) where V is a set of vertices and E is a set of edges, each edge is a pair of vertices.
Explanation
The lesson defines graph as ordered pair of vertices and edges.
- 43
What is the difference between a directed graph and an undirected graph?
Model answer
In a directed graph (digraph), edges are directed (ordered pairs); in an undirected graph, edges are undirected (unordered pairs) and bidirectional.
Explanation
The lesson distinguishes directed and undirected graphs.
- 44
What is a simple graph?
Model answer
A graph with no self-loops and no multiple edges between the same pair of vertices.
Explanation
The lesson defines simple graph as having no loops or multi-edges.
- 45
What is a path in a graph?
Model answer
A sequence of vertices where each adjacent pair is connected by an edge. Usually, a simple path has no repeated vertices.
Explanation
The lesson defines path as a sequence with edges between consecutive vertices; simple path has no repetitions.
- 46
What does it mean for a graph to be connected (undirected) or strongly connected (directed)?
Model answer
An undirected graph is connected if there is a path between any two vertices. A directed graph is strongly connected if there is a directed path between any two vertices.
Explanation
The lesson explains connectivity for undirected and strong connectivity for directed graphs.
- 47
What is an adjacency matrix representation of a graph?
Model answer
A 2D array of size V x V where entry (i, j) is 1 (or weight) if there is an edge from vertex i to vertex j, and 0 (or infinity) otherwise.
Explanation
The lesson describes adjacency matrix as a VxV matrix storing edge existence or weight.
- 48
What is an adjacency list representation of a graph?
Model answer
An array of lists, one per vertex, where each list stores the neighbors (and possibly edge weights) of that vertex. Often implemented as linked lists or dynamic arrays.
Explanation
The lesson describes adjacency list as an array of lists for each vertex's neighbors.
- 49
When is adjacency list preferred over adjacency matrix?
Model answer
When the graph is sparse (number of edges is much less than V^2), adjacency list saves memory and may be faster for operations like finding neighbors.
Explanation
The lesson compares both and notes that for sparse graphs, adjacency list is better in space and time.
- 50
What is the time complexity to find all neighbors of a given vertex in an adjacency matrix?
Model answer
O(V), because we must scan the entire row of the vertex.
Explanation
The lesson states that finding adjacent nodes requires scanning a row of size V.
- 51
What is the time complexity to check if an edge exists between two given vertices in an adjacency matrix?
Model answer
O(1) if indices are known, because we directly access the cell.
Explanation
The lesson explains that with adjacency matrix, checking an edge is constant time by accessing A[i][j].

Ask anything
We've read this whole study set, so questions about it get the best answers.