Data Structures Course
- 87Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What is the time complexity of accessing an element by index in a static array?
What is the time complexity of accessing an element by index in a static array?
87 questions
- 1
What is the time complexity of accessing an element by index in a static array?
- A
O(logn)
- B
O(n2)
- C
O(1)
- D
O(n)
Correct answer
CO(1)
Explanation
Array elements are stored in contiguous memory locations, so the address of index i is computed as base address +i× element size, allowing direct access in constant time.
- A
- 2
Why does accessing an element by index in an array take O(1) time?
- A
Because the array is stored in contiguous memory and the address can be computed arithmetically
- B
Because the array is stored in a linked list structure
- C
Because the array always has a fixed size
- D
Because the array uses a hash function to locate elements
Correct answer
ABecause the array is stored in contiguous memory and the address can be computed arithmetically
Explanation
Contiguous memory allocation allows direct calculation of the memory address of any index using the base address and offset, avoiding traversal.
- A
- 3
What is the time complexity of inserting an element at the beginning of a fixed-size array?
- A
O(1)
- B
O(n)
- C
O(n2)
- D
O(logn)
Correct answer
BO(n)
Explanation
Inserting at the beginning requires shifting all existing elements one position to the right to make space, which takes O(n) time where n is the number of elements.
- A
- 4
In a fixed-size array, what happens to the last element when you insert an element in the middle?
- A
It is lost because there is no space
- B
It is placed at the beginning of the array
- C
It remains unchanged
- D
It is moved to the next index
Correct answer
AIt is lost because there is no space
Explanation
Since the array has a fixed size, inserting in the middle requires shifting elements to the right, causing the last element to be overwritten and lost.
- A
- 5
What typically happens when a dynamic array runs out of capacity?
- A
The array automatically compresses its elements
- B
The program crashes
- C
A new larger array is allocated, elements are copied, and the old array is discarded
- D
Elements are stored in a linked list instead
Correct answer
CA new larger array is allocated, elements are copied, and the old array is discarded
Explanation
Dynamic arrays often double their size when full, allocate new memory, copy existing elements, and free the old array to accommodate more elements.
- A
- 6
What is the amortized time complexity of appending an element to a dynamic array?
- A
O(1)
- B
O(n2)
- C
O(n)
- D
O(logn)
Correct answer
AO(1)
Explanation
Although occasional resizing costs O(n), the cost is spread over many insertions, resulting in O(1) amortized time per append.
- A
- 7
What is the primary advantage of a dynamic array over a static array?
- A
It uses less memory
- B
It can grow and shrink in size as needed
- C
It does not require contiguous memory
- D
It has faster access time
Correct answer
BIt can grow and shrink in size as needed
Explanation
Dynamic arrays can resize automatically, allowing them to accommodate varying numbers of elements without predefined capacity.
- A
- 8
What is the time complexity of initializing a fixed-size array of size n?
- A
O(1)
- B
O(logn)
- C
O(n)
- D
O(n2)
Correct answer
CO(n)
Explanation
Initializing a fixed-size array requires allocating and setting each of the n memory slots, which takes O(n) time.
- A
- 9
In an array, how is the memory address of an element at index i computed?
- A
base address ×i
- B
base address +i× size of element
- C
base address +(i−1)× size of element
- D
base address +i
Correct answer
Bbase address +i× size of element
Explanation
Contiguous memory layout allows the address of index i to be calculated as the base address plus the offset i times the element size.
- A
- 10
Which of the following operations on a static array takes O(1) time?
- A
Inserting an element at the beginning
- B
Deleting an element from the middle
- C
Accessing an element by index
- D
Inserting an element in the middle
Correct answer
CAccessing an element by index
Explanation
Accessing by index uses direct address calculation and is independent of array size, making it O(1).
- A
- 11
What does each node in a singly linked list contain?
- A
A value, a next pointer, and a previous pointer
- B
A value and a pointer to the next node
- C
A value and a pointer to the previous node
- D
A value and an index
Correct answer
BA value and a pointer to the next node
Explanation
A singly linked list node consists of a value (data) and a 'next' pointer that references the subsequent node in the list.
- A
- 12
In a singly linked list, how do you access the last element?
- A
By accessing the node stored at a special memory address
- B
By traversing from the head until the next pointer is null
- C
By directly using the tail pointer
- D
By using the index of the last node
Correct answer
BBy traversing from the head until the next pointer is null
Explanation
Since there is no direct tail reference in a basic singly linked list, you must traverse from the head node, following next pointers until reaching a node whose next is null.
- A
- 13
What is the time complexity of inserting a node at the beginning of a singly linked list (with a head pointer)?
- A
O(n)
- B
O(n2)
- C
O(logn)
- D
O(1)
Correct answer
DO(1)
Explanation
Inserting at the beginning involves creating a new node, setting its next to the current head, and updating the head pointer—all constant-time operations.
- A
- 14
In a singly linked list with only a head pointer, why does inserting at the end take O(n) time?
- A
Because the head pointer must be updated
- B
Because you must traverse the entire list to find the last node
- C
Because you need to shift all elements
- D
Because you need to resize the list
Correct answer
BBecause you must traverse the entire list to find the last node
Explanation
Without a tail pointer, you must start at the head and follow next pointers until you reach the last node, which requires O(n) steps.
- A
- 15
How does a circular singly linked list improve insertion/deletion at the end?
- A
By using an array for storage
- B
By storing a tail pointer that points to the last node
- C
By increasing the size of each node
- D
By using a doubly linked list structure
Correct answer
BBy storing a tail pointer that points to the last node
Explanation
In a circular singly linked list, we can maintain a tail pointer. Since the tail's next points to the head, inserting at the end becomes O(1) by updating the tail's next.
- A
- 16
In a circular singly linked list, what does the next pointer of the tail node point to?
- A
null
- B
The head node
- C
Itself
- D
The previous node
Correct answer
BThe head node
Explanation
To make the list circular, the last node's next pointer references the head node, forming a cycle.
- A
- 17
What is the main advantage of a doubly linked list over a singly linked list?
- A
It allows traversal in both directions and constant-time deletion when node is known
- B
It uses less memory
- C
It does not require pointers
- D
It has faster access by index
Correct answer
AIt allows traversal in both directions and constant-time deletion when node is known
Explanation
Doubly linked lists have a prev pointer, enabling backward traversal and O(1) deletion of a given node because the previous node is directly accessible.
- A
- 18
If you have a pointer to a node in a doubly linked list, what is the time complexity of deleting that node?
- A
O(1)
- B
O(logn)
- C
O(n2)
- D
O(n)
Correct answer
AO(1)
Explanation
With access to the node, you can update its previous node's next and its next node's prev in constant time, then free the node.
- A
- 19
What is the disadvantage of a doubly linked list compared to a singly linked list?
- A
It uses more memory per node due to the extra prev pointer
- B
It does not support deletion
- C
It is slower for traversal
- D
It cannot be made circular
Correct answer
AIt uses more memory per node due to the extra prev pointer
Explanation
Each node in a doubly linked list stores an additional prev pointer, increasing memory usage compared to a singly linked list.
- A
- 20
How would you implement a queue using a singly linked list to achieve O(1) enqueue and dequeue?
- A
Store only a head pointer and enqueue at head, dequeue at tail
- B
Store both head and tail pointers; enqueue at tail, dequeue at head
- C
Use an array instead of a linked list
- D
Store only a tail pointer and use circular references
Correct answer
BStore both head and tail pointers; enqueue at tail, dequeue at head
Explanation
With head and tail pointers, enqueue (add at tail) can be done in O(1) by updating tail's next, and dequeue (remove from head) in O(1) by updating head.
- A
- 21
What does FIFO stand for in the context of queues?
- A
First In, First Out
- B
Fast Input, Fast Output
- C
File Input/Output
- D
First In, Last Out
Correct answer
AFirst In, First Out
Explanation
FIFO is the fundamental property of a queue: the first element added is the first one to be removed.
- A
- 22
What is the time complexity of enqueue operation in a queue implemented with a singly linked list with head and tail pointers?
- A
O(1)
- B
O(logn)
- C
O(n)
- D
O(n2)
Correct answer
AO(1)
Explanation
Enqueue adds an element at the tail; with a tail pointer, we can directly link the new node after the tail in constant time.
- A
- 23
Why is using an array to implement a queue problematic for dequeue?
- A
Because dequeue from the front requires shifting all remaining elements, which takes O(n) time
- B
Because arrays have fixed size
- C
Because arrays are not linear data structures
- D
Because arrays do not support removing elements
Correct answer
ABecause dequeue from the front requires shifting all remaining elements, which takes O(n) time
Explanation
When the front of the array is used as the queue front, removing an element causes a gap, and shifting all subsequent elements to fill it takes O(n) time.
- A
- 24
In a stack, what is the order of element removal?
- A
First In, First Out
- B
Last In, First Out
- C
Sorted order
- D
Random order
Correct answer
BLast In, First Out
Explanation
A stack follows LIFO (Last In, First Out): the most recently added element is the first to be removed.
- A
- 25
What data structure can be used to implement a stack with O(1) push and pop?
- A
Singly linked list (operating at the head)
- B
Queue
- C
Array (operating at the end)
- D
Both array (end) and singly linked list (head)
Correct answer
DBoth array (end) and singly linked list (head)
Explanation
If we push and pop at the end of an array, both operations are O(1). Similarly, pushing and popping at the head of a singly linked list is O(1).
- A
- 26
How can two stacks be used to implement a queue?
- A
One stack for input and one for output; during enqueue, transfer all elements to output, push new element, then transfer back to input
- B
It is not possible to implement a queue with two stacks
- C
By using both stacks simultaneously for each operation
- D
One stack for even elements and one for odd elements
Correct answer
AOne stack for input and one for output; during enqueue, transfer all elements to output, push new element, then transfer back to input
Explanation
The common method uses an input stack and an output stack. For enqueue, elements are moved to the output stack (reversing order), the new element is pushed onto the input stack, then elements are moved back, maintaining FIFO order.
- A
- 27
In the two-stack queue implementation, which operation typically becomes O(n)?
- A
peek
- B
isEmpty
- C
dequeue
- D
enqueue
Correct answer
Denqueue
Explanation
Enqueue requires moving all elements from the input stack to the output stack and back, which takes O(n) time in the worst case.
- A
- 28
What is a real-world example of a stack?
- A
A playlist of songs played in order
- B
A list of tasks to be done in any order
- C
A pile of plates where you take from the top
- D
A line of people waiting for service
Correct answer
CA pile of plates where you take from the top
Explanation
A stack of plates: the last plate placed on top is the first one removed, which matches the LIFO property of a stack.
- A
- 29
What operation in a queue returns the front element without removing it?
- A
push
- B
peek
- C
size
- D
pop
Correct answer
Bpeek
Explanation
Peek (also called front) returns the element at the front of the queue without dequeuing it, allowing inspection without modification.
- A
- 30
What property must be maintained for both push and pop operations in a stack?
- A
They must be recursive
- B
They must be performed on opposite ends
- C
They must be performed on the same end of the data structure
- D
They must use O(1) space
Correct answer
CThey must be performed on the same end of the data structure
Explanation
In a stack, both push (add) and pop (remove) are done at the same end (the top) to maintain LIFO order.
- A
- 31
What is the maximum number of children a node can have in a binary tree?
- A
unlimited
- B
3
- C
1
- D
2
Correct answer
D2
Explanation
A binary tree is defined as a tree where each node has at most two children, often referred to as left and right child.
- A
- 32
What is a leaf node in a binary tree?
- A
The root node
- B
Any node that is not a leaf
- C
A node with exactly two children
- D
A node with no children
Correct answer
DA node with no children
Explanation
A leaf node (or external node) has no children, meaning both left and right pointers are null.
- A
- 33
What is the height of a binary tree?
- A
The maximum number of edges on a path from the root to a leaf
- B
The number of nodes in the tree
- C
The number of levels
- D
The depth of the root node
Correct answer
AThe maximum number of edges on a path from the root to a leaf
Explanation
Height is defined as the longest path from the root to a leaf, measured by the number of edges along that path.
- A
- 34
In a binary tree, what is the depth of the root node?
- A
0
- B
-1
- C
2
- D
1
Correct answer
A0
Explanation
The depth of a node is the number of edges from the root to that node. Since the root is at the top, its depth is 0.
- A
- 35
What is the maximum number of nodes in a binary tree of height h?
- A
h2
- B
2h
- C
2h−1
- D
2h+1−1
Correct answer
D2h+1−1
Explanation
A full binary tree of height h has the most nodes: 2h+1−1 because each level i (0-indexed) has 2i nodes.
- A
- 36
What traversal method uses a queue to process nodes level by level?
- A
Depth-first search
- B
Breadth-first search
- C
Post-order traversal
- D
In-order traversal
Correct answer
BBreadth-first search
Explanation
Breadth-first search (BFS) processes all nodes at the current depth before moving to the next level, typically implemented with a queue.
- A
- 37
In a depth-first traversal of a binary tree, what data structure is used?
- A
Queue
- B
Stack
- C
Array
- D
Linked list
Correct answer
BStack
Explanation
Depth-first traversal uses a stack (explicitly or via recursion) to go deep into the tree before backtracking.
- A
- 38
What is the order of nodes visited in an in-order traversal?
- A
Right subtree, current node, left subtree
- B
Left subtree, current node, right subtree
- C
Current node, left subtree, right subtree
- D
Left subtree, right subtree, current node
Correct answer
BLeft subtree, current node, right subtree
Explanation
In-order traversal processes the left subtree first, then the current node, then the right subtree.
- A
- 39
What defines a full binary tree?
- A
All nodes have either 0 or 2 children
- B
Every node has exactly two children
- C
The tree is balanced
- D
All levels are completely filled
Correct answer
AAll nodes have either 0 or 2 children
Explanation
A full binary tree is one where every node has either 0 or 2 children (no node has only one child).
- A
- 40
What defines a complete binary tree?
- A
The height is minimal
- B
All levels are completely filled
- C
Every node has two children
- D
All levels except possibly the last are full, and nodes in the last level are as far left as possible
Correct answer
DAll levels except possibly the last are full, and nodes in the last level are as far left as possible
Explanation
A complete binary tree is filled level by level from left to right, with the last level having nodes as left as possible.
- A
- 41
What defines a balanced binary tree?
- A
All leaves are at the same depth
- B
The tree is full
- C
The difference in height between left and right subtrees of any node is at most 1
- D
The root has equal number of nodes on both sides
Correct answer
CThe difference in height between left and right subtrees of any node is at most 1
Explanation
A balanced binary tree (height-balanced) ensures that for every node, the height of its left and right subtrees differ by no more than one.
- A
- 42
What is a perfect binary tree?
- A
A tree where the height is minimal
- B
A tree that is both full and complete
- C
A tree where all levels are completely filled
- D
A tree where all nodes have 0 or 2 children
Correct answer
CA tree where all levels are completely filled
Explanation
A perfect binary tree is one in which all internal nodes have two children and all leaves are at the same depth, meaning every level is completely filled.
- A
- 43
In a binary tree, what is the time complexity of inserting a node using level-order (BFS) traversal?
- A
O(n)
- B
O(n2)
- C
O(logn)
- D
O(1)
Correct answer
AO(n)
Explanation
BFS traversal visits each node until finding the first node with an empty child slot; in the worst case, it may need to visit all n nodes, hence O(n).
- A
- 44
When deleting a node from a binary tree, why is the deepest rightmost node chosen as the replacement?
- A
It is the root of the tree
- B
It is the largest value in the tree
- C
It is a leaf node and can be easily removed without affecting other children
- D
It is the node with the highest index
Correct answer
CIt is a leaf node and can be easily removed without affecting other children
Explanation
The deepest rightmost node is a leaf node (or has at most one child), so swiping its value with the node to delete and then removing it is straightforward and maintains tree structure.
- A
- 45
How many edges are there in a tree with n nodes?
- A
n
- B
n+1
- C
n−1
- D
2n
Correct answer
Cn−1
Explanation
A tree with n nodes always has exactly n−1 edges because it is a connected acyclic graph.
- A
- 46
What is the property of a binary search tree?
- A
For any node, all values in its left subtree are less, and all values in its right subtree are greater than the node's value
- B
All nodes are unique
- C
The tree is balanced
- D
Every node has exactly two children
Correct answer
AFor any node, all values in its left subtree are less, and all values in its right subtree are greater than the node's value
Explanation
The BST property ensures that the tree allows efficient searching: left subtree contains smaller values, right subtree contains larger values.
- A
- 47
What is the time complexity of searching for a value in a balanced BST?
- A
O(logn)
- B
O(1)
- C
O(n)
- D
O(nlogn)
Correct answer
AO(logn)
Explanation
In a balanced BST, the height is log2n, so each comparison eliminates half the tree, leading to O(logn) time.
- A
- 48
What is the worst-case time complexity of searching in a BST?
- A
O(1)
- B
O(n2)
- C
O(logn)
- D
O(n)
Correct answer
DO(n)
Explanation
If the BST becomes skewed (like a linked list), the height becomes n, so searching may require visiting all nodes, resulting in O(n) time.
- A
- 49
During BST insertion, where is the new node always placed?
- A
As an internal node
- B
As a leaf node
- C
At the root
- D
At a random position
Correct answer
BAs a leaf node
Explanation
Insertion always adds the new node as a leaf, found by traversing the tree following the BST property until an empty spot is reached.
- A
- 50
In BST deletion, what is the first case (simplest scenario)?
- A
Deleting a node with one child
- B
Deleting the root node
- C
Deleting a leaf node
- D
Deleting a node with two children
Correct answer
CDeleting a leaf node
Explanation
Deleting a leaf node is simplest: you just remove the reference from its parent, which takes O(1) after locating the node.
- A
- 51
In BST deletion, what is the second case (node with one child)?
- A
Use recursion on both subtrees
- B
Swap with the maximum in left subtree
- C
Delete both children
- D
Replace the node with its child by linking parent to child
Correct answer
DReplace the node with its child by linking parent to child
Explanation
If the node has one child, you can bypass it by connecting its parent directly to that child, preserving the BST property.
- A
- 52
In BST deletion, what is the third case (node with two children)?
- A
Delete both children and then the node
- B
Replace the node with a leaf node from its subtree and delete that leaf
- C
Make the left child the new root of the subtree
- D
Randomly choose a child to keep
Correct answer
BReplace the node with a leaf node from its subtree and delete that leaf
Explanation
For two children, you find either the maximum in the left subtree or the minimum in the right subtree, copy its value to the node, then delete that leaf (or one-child) node.
- A
- 53
When deleting a node with two children, why is the maximum of the left subtree chosen as the replacement?
- A
It is randomly selected
- B
It is the largest value smaller than the node, preserving BST property
- C
It is the leftmost node
- D
It is the node with the smallest value in the left subtree
Correct answer
BIt is the largest value smaller than the node, preserving BST property
Explanation
The maximum in the left subtree is the greatest value that is still less than the node's value, so replacing keeps the BST order intact.
- A
- 54
What does the height of a BST depend on?
- A
The size of the elements
- B
The data type of the elements
- C
The programming language used
- D
The order in which elements are inserted
Correct answer
DThe order in which elements are inserted
Explanation
If elements are inserted in sorted order, the tree becomes skewed (height n); if inserted in random order, height tends to O(logn).
- A
- 55
What is a self-balancing BST?
- A
A BST that only allows leaf deletions
- B
A BST that uses a hash function
- C
A BST that stores data in an array
- D
A BST that automatically maintains a balanced height after insertions and deletions
Correct answer
DA BST that automatically maintains a balanced height after insertions and deletions
Explanation
Self-balancing BSTs (e.g., AVL trees, Red-Black trees) perform rotations or adjustments to keep the tree height O(logn).
- A
- 56
What is a max-heap property?
- A
Every parent node is less than or equal to its children
- B
The tree is balanced
- C
Every parent node is greater than or equal to its children
- D
The root is the smallest element
Correct answer
CEvery parent node is greater than or equal to its children
Explanation
In a max-heap, the value of each node is at least as large as the values of its children, so the maximum is at the root.
- A
- 57
What data structure is commonly used to implement a heap?
- A
Graph
- B
Linked list
- C
Queue
- D
Array
Correct answer
DArray
Explanation
Heaps are efficiently implemented using arrays because the complete binary tree structure allows easy calculation of parent and child indices.
- A
- 58
In a heap represented as an array, how do you find the left child of a node at index i?
- A
2i+2
- B
i−1
- C
i/2
- D
2i+1
Correct answer
D2i+1
Explanation
For a zero-indexed array, the left child of node at index i is at index 2i+1 (if within bounds).
- A
- 59
In a heap represented as an array, how do you find the parent of a node at index i?
- A
⌊i/2⌋
- B
i−1
- C
⌊(i−1)/2⌋
- D
2i
Correct answer
C⌊(i−1)/2⌋
Explanation
The parent index is found by ⌊(i−1)/2⌋, which for zero-indexed arrays gives the correct parent.
- A
- 60
What is the time complexity of inserting a new element into a heap?
- A
O(logn)
- B
O(1)
- C
O(n)
- D
O(nlogn)
Correct answer
AO(logn)
Explanation
Insertion adds the element at the end and then sifts it up (bubbles up) to restore heap property. The number of swaps is at most the height, O(logn).
- A
- 61
What operation is performed after inserting at the end of a heap to restore the heap property?
- A
sift-down
- B
heapify
- C
reheap
- D
sift-up
Correct answer
Dsift-up
Explanation
After adding a new element at the last position, we sift it up (or bubble up) by swapping with its parent until the heap property is satisfied.
- A
- 62
What is the time complexity of extracting the maximum element from a max-heap?
- A
O(logn)
- B
O(n)
- C
O(nlogn)
- D
O(1)
Correct answer
AO(logn)
Explanation
Extracting the max involves replacing the root with the last element and then sifting down, which takes O(logn) time.
- A
- 63
After extracting the maximum from a max-heap, what is typically done to the heap?
- A
The root is deleted and the heap remains unchanged
- B
The heap is rebuilt from scratch
- C
All elements are shifted left
- D
The last element is moved to the root and then sifted down
Correct answer
DThe last element is moved to the root and then sifted down
Explanation
To maintain the complete tree, the last element is placed at the root, then sifted down to restore the heap property.
- A
- 64
How is a heap built from an unsorted array of n elements?
- A
By repeatedly inserting each element into an empty heap, which takes O(nlogn) time
- B
By sorting the array first
- C
By dividing and conquering
- D
By using a binary search tree
Correct answer
ABy repeatedly inserting each element into an empty heap, which takes O(nlogn) time
Explanation
One common method is to start with an empty heap and insert each element one by one, each insertion costing O(logn), totaling O(nlogn).
- A
- 65
What is heap sort?
- A
A sorting algorithm that uses binary search
- B
A sorting algorithm that compares adjacent elements
- C
A sorting algorithm that divides the array into halves
- D
A sorting algorithm that builds a heap and repeatedly extracts the maximum/minimum element
Correct answer
DA sorting algorithm that builds a heap and repeatedly extracts the maximum/minimum element
Explanation
Heap sort first builds a heap from the input, then repeatedly removes the root (max or min) and inserts it into the sorted output, achieving O(nlogn).
- A
- 66
What is the time complexity of heap sort?
- A
O(n)
- B
O(nlogn)
- C
O(logn)
- D
O(n2)
Correct answer
BO(nlogn)
Explanation
Heap sort consists of building a heap (O(nlogn)) and then n extractions (O(nlogn)), so total is O(nlogn).
- A
- 67
In a min-heap, where is the minimum element located?
- A
At a leaf
- B
At the last index
- C
It could be anywhere
- D
At the root
Correct answer
DAt the root
Explanation
By definition, in a min-heap every parent is less than or equal to its children, so the smallest element is always at the root.
- A
- 68
What is a vertex in a graph?
- A
A node or point in the graph
- B
A cycle
- C
A connection between two nodes
- D
A path between nodes
Correct answer
AA node or point in the graph
Explanation
Vertices (or nodes) are the fundamental units of a graph that can be connected by edges.
- A
- 69
What is an edge in a graph?
- A
A traversal algorithm
- B
A connection between two vertices
- C
A node in the graph
- D
A data structure
Correct answer
BA connection between two vertices
Explanation
Edges represent relationships or connections between pairs of vertices.
- A
- 70
What is an undirected graph?
- A
A graph without weights
- B
A graph where edges have no direction
- C
A graph with cycles
- D
A graph where edges have arrows
Correct answer
BA graph where edges have no direction
Explanation
In an undirected graph, edges are bidirectional; the pair (u,v) is the same as (v,u).
- A
- 71
What is a directed graph?
- A
A graph where edges have a direction
- B
A graph where all vertices are connected
- C
A graph with weights on edges
- D
A graph that is a tree
Correct answer
AA graph where edges have a direction
Explanation
In a directed graph (digraph), each edge has a specific direction from one vertex to another.
- A
- 72
What is a weighted graph?
- A
A graph that is balanced
- B
A graph where edges have associated numeric values
- C
A graph with many vertices
- D
A graph with no cycles
Correct answer
BA graph where edges have associated numeric values
Explanation
Weighted graphs assign a weight (cost, length, etc.) to each edge, often used for optimization problems.
- A
- 73
What is a cycle in a directed graph?
- A
A path with no repeated vertices
- B
A set of edges that form a loop
- C
A path that visits every vertex exactly once
- D
A path that starts and ends at the same vertex following directed edges
Correct answer
DA path that starts and ends at the same vertex following directed edges
Explanation
A cycle is a sequence of vertices where the first and last are the same, and each consecutive pair is connected by a directed edge.
- A
- 74
What is an acyclic graph?
- A
A graph that contains no cycles
- B
A graph that is disconnected
- C
A graph that has only one path between vertices
- D
A graph that is complete
Correct answer
AA graph that contains no cycles
Explanation
An acyclic graph (e.g., a tree or DAG) has no cycles, meaning there is no way to return to a vertex by following edges.
- A
- 75
What is an adjacency matrix?
- A
A 2D array where cell (i,j) indicates the presence or weight of an edge from vertex i to j
- B
A hash table of vertices
- C
A list of edges
- D
A tree representation
Correct answer
AA 2D array where cell (i,j) indicates the presence or weight of an edge from vertex i to j
Explanation
An adjacency matrix is a square matrix of size V×V where entry aij represents the edge between vertex i and j.
- A
- 76
What is an adjacency list?
- A
A hash set of edges
- B
A linked list of vertices
- C
A collection of lists where each vertex has a list of its adjacent vertices
- D
A 2D array of edges
Correct answer
CA collection of lists where each vertex has a list of its adjacent vertices
Explanation
Adjacency list uses an array of lists; for each vertex, a list stores the vertices it is connected to (and possibly edge weights).
- A
- 77
Which representation is more space-efficient for a sparse graph?
- A
Adjacency matrix
- B
Both are equally efficient
- C
Incidence matrix
- D
Adjacency list
Correct answer
DAdjacency list
Explanation
Adjacency list uses O(V+E) space, while adjacency matrix uses O(V2) space, making the list more efficient for sparse graphs.
- A
- 78
Which representation allows O(1) edge existence check?
- A
Adjacency matrix
- B
Adjacency list
- C
Both
- D
Neither
Correct answer
AAdjacency matrix
Explanation
In an adjacency matrix, checking if edge (i,j) exists is done by accessing cell (i,j) in O(1) time, whereas adjacency list may require traversal.
- A
- 79
What is a hash function?
- A
A function that compresses data
- B
A function that maps input data to a fixed-size output, usually an integer
- C
A function that sorts data
- D
A function that encrypts data
Correct answer
BA function that maps input data to a fixed-size output, usually an integer
Explanation
A hash function takes an input (key) and returns a fixed-size value, often used to index into a hash table.
- A
- 80
What is a hash collision?
- A
When the hash table is full
- B
When the hash function runs out of memory
- C
When two different inputs produce the same hash output
- D
When the hash output is too large
Correct answer
CWhen two different inputs produce the same hash output
Explanation
A collision occurs when the hash function maps two distinct keys to the same index, requiring a resolution strategy.
- A
- 81
Why is it desirable for a hash function to produce unique outputs?
- A
To reduce memory usage
- B
To avoid collisions and maintain O(1) average operation time
- C
To ensure security
- D
To make the function faster
Correct answer
BTo avoid collisions and maintain O(1) average operation time
Explanation
Unique outputs prevent collisions, which would otherwise degrade performance due to resolution overhead, keeping lookups O(1) on average.
- A
- 82
How does hashing enable constant-time lookup in a hash table?
- A
The hash table uses binary search
- B
The hash function computes an index where the key-value pair is stored, allowing direct access
- C
The hash table uses linked lists
- D
The hash function sorts the keys
Correct answer
BThe hash function computes an index where the key-value pair is stored, allowing direct access
Explanation
The hash function generates an index directly from the key, so the table can go to that index in O(1) time to retrieve the value.
- A
- 83
What common data structure uses hashing?
- A
Linked list
- B
Queue
- C
Hash map (dictionary)
- D
Stack
Correct answer
CHash map (dictionary)
Explanation
Hash maps (also called hash tables or dictionaries) rely on hash functions to map keys to indices for efficient storage and retrieval.
- A
- 84
What happens when a hash collision occurs?
- A
A collision resolution strategy is used, such as chaining or open addressing
- B
The hash function is recalculated
- C
The hash table crashes
- D
The new element is discarded
Correct answer
AA collision resolution strategy is used, such as chaining or open addressing
Explanation
Collisions are handled by techniques like chaining (linked list per bucket) or open addressing (probing for next free slot) to store multiple items at the same index.
- A
- 85
What is a perfect hash function?
- A
A hash function that uses the least memory
- B
A hash function that is fastest to compute
- C
A hash function that maps each input to a unique output with no collisions
- D
A hash function that works for any input size
Correct answer
CA hash function that maps each input to a unique output with no collisions
Explanation
A perfect hash function guarantees that different keys map to distinct indices, eliminating collisions entirely for a given set of keys.
- A
- 86
In practice, why is it difficult to achieve perfect hashing?
- A
Because the input set is often unknown or dynamic, and the output range is limited
- B
Because hash functions are not deterministic
- C
Because hash functions are too fast
- D
Because computers have limited processing power
Correct answer
ABecause the input set is often unknown or dynamic, and the output range is limited
Explanation
Perfect hashing requires knowledge of all keys in advance and a large enough range; for dynamic or large sets, collisions are inevitable.
- A
- 87
What is a digest in the context of hashing?
- A
The input to a hash function
- B
A type of hash table
- C
A collision resolution method
- D
The output of a hash function
Correct answer
DThe output of a hash function
Explanation
Digest refers to the fixed-length result produced by a hash function from an input message.
- A

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