JavaScript Data Structures
- 86Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
Which principle describes how a stack processes elements?
Which principle describes how a stack processes elements?
86 questions
- 1
Which principle describes how a stack processes elements?
- A
Highest priority, first out
- B
Last in, first out
- C
Random access, first out
- D
First in, first out
Correct answer
BLast in, first out
Explanation
A stack uses last-in, first-out behavior: the most recently added element is the first one removed.
- A
- 2
Which stack operation adds an element to the top of the stack?
- A
shift
- B
pop
- C
push
- D
peek
Correct answer
Cpush
Explanation
The push operation places a new element on top of a stack.
- A
- 3
What does the pop operation do to a stack?
- A
Removes and returns the top element
- B
Displays the top element without removing it
- C
Counts the elements without changing them
- D
Adds an element to the bottom
Correct answer
ARemoves and returns the top element
Explanation
Pop removes the top element and returns its value.
- A
- 4
How does the peek operation differ from pop?
- A
Peek returns the bottom element and removes it
- B
Peek adds an element to the top of the stack
- C
Peek removes the top element without returning it
- D
Peek returns the top element without removing it
Correct answer
DPeek returns the top element without removing it
Explanation
Peek examines the top element while leaving it in the stack; pop removes it.
- A
- 5
Why can a JavaScript array be used directly as a stack?
- A
Arrays automatically prevent duplicate values
- B
Arrays provide operations such as push and pop
- C
Arrays allow access only to the last element
- D
Arrays always maintain sorted order
Correct answer
BArrays provide operations such as push and pop
Explanation
JavaScript arrays already provide the push and pop operations needed for basic stack behavior.
- A
- 6
Why does popping letters after pushing them in forward order produce a reversed word?
- A
The stack automatically alphabetizes the letters
- B
The last pushed letter is removed first
- C
The first pushed letter is removed first
- D
The stack converts each letter to its opposite
Correct answer
BThe last pushed letter is removed first
Explanation
Because a stack is last in, first out, letters pushed from left to right are popped from right to left.
- A
- 7
How can a stack-based palindrome check determine whether a word is a palindrome?
- A
It sorts the letters and compares their frequencies
- B
It compares only the first and last letters
- C
It compares the original word with the reversed word
- D
It checks whether every letter appears only once
Correct answer
CIt compares the original word with the reversed word
Explanation
A palindrome reads the same forward and backward, so comparing the original word with its stack-generated reverse is sufficient.
- A
- 8
What does the count variable track in the custom stack implementation?
- A
The value of the top element
- B
The number of elements in the stack
- C
The index of the bottom element
- D
The number of duplicate elements
Correct answer
BThe number of elements in the stack
Explanation
The custom stack uses count to record how many items it currently contains.
- A
- 9
What happens when pop is called on an empty custom stack?
- A
It throws away the entire stack
- B
It adds a null element
- C
It returns the bottom element
- D
It returns undefined
Correct answer
DIt returns undefined
Explanation
The implementation checks whether the count is zero and returns undefined when no element exists.
- A
- 10
Which principle describes a standard queue?
- A
Highest value, first out
- B
Random element, first out
- C
Last in, first out
- D
First in, first out
Correct answer
DFirst in, first out
Explanation
A queue removes elements in the same order in which they were added.
- A
- 11
In the described array-based queue, where are elements added and removed?
- A
Added and removed only at the beginning
- B
Added at the end and removed from the beginning
- C
Added at the beginning and removed from the end
- D
Added and removed only at the end
Correct answer
BAdded at the end and removed from the beginning
Explanation
The implementation uses push to add at the end and shift to remove from the beginning.
- A
- 12
What does the front operation of a queue do?
- A
Returns the last element without removing it
- B
Returns the first element without removing it
- C
Adds a new element before the first element
- D
Removes and returns the first element
Correct answer
BReturns the first element without removing it
Explanation
Front examines the element at the beginning of the queue while leaving it in place.
- A
- 13
How does a priority queue differ from a standard queue?
- A
Elements are ordered partly by priority
- B
Elements must always be strings
- C
Elements cannot be removed from a priority queue
- D
Elements are arranged alphabetically
Correct answer
AElements are ordered partly by priority
Explanation
A priority queue stores both an element and a priority, placing higher-priority elements ahead of lower-priority ones.
- A
- 14
If two elements in a priority queue have the same priority, how are they ordered?
- A
They are ordered alphabetically
- B
They retain their insertion order
- C
They are placed at random
- D
They are ordered from largest to smallest
Correct answer
BThey retain their insertion order
Explanation
Equal-priority elements behave like elements in a normal queue and remain in insertion order.
- A
- 15
What is the defining property of a set data structure?
- A
It stores values only in first-in, first-out order
- B
It stores unique values without requiring a particular order
- C
It stores every value together with a numerical index
- D
It stores values only in last-in, first-out order
Correct answer
BIt stores unique values without requiring a particular order
Explanation
A set contains no duplicate elements, and its values are not required to have a particular order.
- A
- 16
What does the has method of a set return?
- A
The number of duplicate elements
- B
A sorted copy of the set
- C
Whether a specified element is present
- D
The position of the largest element
Correct answer
CWhether a specified element is present
Explanation
The has method checks for membership and returns true or false.
- A
- 17
What should add return when an element is already in the custom set?
- A
The duplicate element
- B
true
- C
The set's size
- D
false
Correct answer
Dfalse
Explanation
The custom add method returns false when it does not add an element because that element already exists.
- A
- 18
What should remove return when it successfully deletes an element from the custom set?
- A
The deleted element's index
- B
true
- C
false
- D
The entire collection
Correct answer
Btrue
Explanation
The custom remove method returns true after finding and deleting the requested element.
- A
- 19
Which name does the ES6 Set use for the operation called remove in the custom implementation?
- A
erase
- B
discard
- C
removeItem
- D
delete
Correct answer
Ddelete
Explanation
ES6 Set uses delete to remove an element.
- A
- 20
What does the union of two sets contain?
- A
Only elements found in the first set
- B
All elements from both sets without duplicates
- C
Only elements shared by both sets
- D
Only elements found in neither set
Correct answer
BAll elements from both sets without duplicates
Explanation
Union combines both sets and eliminates duplicate values.
- A
- 21
What does the intersection of two sets contain?
- A
Elements present in both sets
- B
Elements present in either set but not both
- C
All elements from the second set
- D
All elements from the first set
Correct answer
AElements present in both sets
Explanation
The intersection contains only values that occur in both sets.
- A
- 22
What does the difference between two sets contain?
- A
All elements from both sets with duplicates
- B
Elements that occur in neither set
- C
Elements in one set but not in the other
- D
Elements occurring in both sets
Correct answer
CElements in one set but not in the other
Explanation
The difference removes from one set the values that are also present in the other set.
- A
- 23
When is one set a subset of another set?
- A
When the first set contains an element absent from the second
- B
When both sets have exactly the same size
- C
When every element of the first set is in the second set
- D
When the sets contain no common elements
Correct answer
CWhen every element of the first set is in the second set
Explanation
A set is a subset when it is completely contained within another set.
- A
- 24
What is a tree node that has no children called?
- A
A branch node
- B
A root node
- C
A parent node
- D
A leaf node
Correct answer
DA leaf node
Explanation
Leaf nodes occur at the ends of a tree and have no children.
- A
- 25
What is the root node of a tree?
- A
The deepest node of the tree
- B
The node with the greatest value
- C
The top node of the tree
- D
The node with no parent but two children always
Correct answer
CThe top node of the tree
Explanation
The root is the topmost node from which the tree branches.
- A
- 26
What distinguishes a binary tree from a general tree?
- A
Each node must have exactly two children
- B
Each node has at most two children
- C
The tree cannot contain leaf nodes
- D
The tree can contain only two total nodes
Correct answer
BEach node has at most two children
Explanation
A binary tree permits zero, one, or two children per node, but never more than two.
- A
- 27
What ordering rule defines a binary search tree?
- A
Only leaf values are required to be ordered
- B
All values are stored in insertion order
- C
Left values are greater than the parent and right values are smaller
- D
Left values are less than or equal to the parent and right values are greater than or equal
Correct answer
DLeft values are less than or equal to the parent and right values are greater than or equal
Explanation
A binary search tree places smaller or equal values in the left subtree and larger or equal values in the right subtree.
- A
- 28
What happens when a value equal to an existing node is added to the described binary search tree?
- A
The entire tree is rebuilt
- B
The duplicate is always placed in the left subtree
- C
The duplicate value is not added
- D
The duplicate is always placed in the right subtree
Correct answer
CThe duplicate value is not added
Explanation
The described insertion function returns without adding a node when the value already exists.
- A
- 29
Where is the minimum value found in a binary search tree?
- A
At the root node
- B
At the leftmost node
- C
At the first inserted node
- D
At the rightmost node
Correct answer
BAt the leftmost node
Explanation
Following left children until no further left child exists reaches the minimum value.
- A
- 30
Where is the maximum value found in a binary search tree?
- A
At the root node
- B
At the shallowest leaf
- C
At the leftmost node
- D
At the rightmost node
Correct answer
DAt the rightmost node
Explanation
Following right children until no further right child exists reaches the maximum value.
- A
- 31
What does the find operation return in the described binary search tree?
- A
The height of the tree
- B
A sorted array of all values
- C
The parent node of the searched value
- D
true if the value exists and false otherwise
Correct answer
Dtrue if the value exists and false otherwise
Explanation
The find function searches the tree and returns a Boolean indicating whether the value is present.
- A
- 32
Which deletion case is simplest in a binary search tree?
- A
Deleting a leaf node
- B
Deleting a node with two children and many descendants
- C
Deleting the root with two children
- D
Deleting a node whose value is duplicated throughout the tree
Correct answer
ADeleting a leaf node
Explanation
A leaf has no children, so it can simply be replaced with null.
- A
- 33
How is a binary search tree node with one child removed?
- A
The node is replaced by its child
- B
Both the node and its child are deleted
- C
The tree is converted into an array
- D
The child is moved to the root automatically
Correct answer
AThe node is replaced by its child
Explanation
When a node has one child, the child takes the node's place in the tree.
- A
- 34
What is the height of a tree node in the described convention?
- A
The number of children it has
- B
Its numerical value minus the root value
- C
The number of values stored in the tree
- D
Its distance in levels from the root
Correct answer
DIts distance in levels from the root
Explanation
The root has height zero, and each lower level increases the distance by one.
- A
- 35
When is a tree considered balanced according to the described criterion?
- A
Every node has exactly two children
- B
Its root is the smallest value
- C
All leaves occur at exactly the same depth
- D
Its minimum and maximum heights differ by at most one
Correct answer
DIts minimum and maximum heights differ by at most one
Explanation
The tree is considered balanced when the minimum and maximum heights differ by no more than one.
- A
- 36
What is the defining order of an in-order traversal?
- A
Node, left subtree, right subtree
- B
Right subtree, node, left subtree
- C
Left subtree, node, right subtree
- D
Left subtree, right subtree, node
Correct answer
CLeft subtree, node, right subtree
Explanation
In-order traversal visits the left subtree, then the current node, then the right subtree.
- A
- 37
What is the defining order of a pre-order traversal?
- A
Right subtree, left subtree, node
- B
Left subtree, node, right subtree
- C
Node, left subtree, right subtree
- D
Left subtree, right subtree, node
Correct answer
CNode, left subtree, right subtree
Explanation
Pre-order traversal visits a node before visiting either of its subtrees.
- A
- 38
What is the defining order of a post-order traversal?
- A
Node, left subtree, right subtree
- B
Left subtree, right subtree, node
- C
Right subtree, node, left subtree
- D
Node, right subtree, left subtree
Correct answer
BLeft subtree, right subtree, node
Explanation
Post-order traversal visits both subtrees before visiting the node itself.
- A
- 39
Which traversal explores all nodes at one level before moving to the next?
- A
In-order traversal
- B
Post-order traversal
- C
Level-order traversal
- D
Pre-order traversal
Correct answer
CLevel-order traversal
Explanation
Level-order traversal is breadth-first: it processes one level completely before proceeding downward.
- A
- 40
What data structure is used in the described level-order traversal?
- A
A queue
- B
A hash table
- C
A set
- D
A stack
Correct answer
AA queue
Explanation
The traversal enqueues child nodes and dequeues them in level order.
- A
- 41
What is the average time complexity of search, insertion, and deletion in a hash table?
- A
O(n2)
- B
O(1)
- C
O(n)
- D
O(logn)
Correct answer
BO(1)
Explanation
Hash tables provide average constant-time search, insertion, and deletion when hashing distributes keys effectively.
- A
- 42
What does a hash function do in a hash table?
- A
Converts an array into a linked list
- B
Removes duplicate values from the table
- C
Sorts all keys alphabetically
- D
Maps a key to an array index
Correct answer
DMaps a key to an array index
Explanation
A hash function transforms a key into a numerical value used to identify a storage bucket or array index.
- A
- 43
What property must a hash function have for reliable lookup?
- A
The result must always equal zero
- B
Every key must produce a different result
- C
The result must depend on insertion order
- D
The same key must produce the same result
Correct answer
DThe same key must produce the same result
Explanation
A key must hash to the same index each time so that its associated value can be found later.
- A
- 44
What is a collision in a hash table?
- A
A value is stored without a key
- B
Two tables have different storage limits
- C
Two keys hash to the same index
- D
A key is removed before it is inserted
Correct answer
CTwo keys hash to the same index
Explanation
A collision occurs when different keys produce the same hash index.
- A
- 45
How can collisions be handled using buckets?
- A
Replace the entire hash table
- B
Discard every key that collides
- C
Store multiple key-value pairs at the same index
- D
Change all keys to numerical indexes
Correct answer
CStore multiple key-value pairs at the same index
Explanation
A bucket can contain multiple key-value pairs, which are searched to locate the requested key.
- A
- 46
Why does a modulus operation constrain a hash index to the available buckets?
- A
The modulus operation converts values into strings
- B
The remainder is always within the bucket-index range
- C
The modulus operation sorts all keys
- D
The modulus operation prevents every collision
Correct answer
BThe remainder is always within the bucket-index range
Explanation
Taking the hash value modulo the number of buckets produces a remainder from zero through one less than the bucket count.
- A
- 47
What two pieces of information does each linked-list node contain?
- A
A parent reference and a child count
- B
An index and a sorting priority
- C
An element and a reference to the next node
- D
A key and a hash value
Correct answer
CAn element and a reference to the next node
Explanation
Each node stores its element and a link to the next node.
- A
- 48
What does the head of a linked list reference?
- A
The node at the middle index
- B
The first node
- C
The last node
- D
The largest node
Correct answer
BThe first node
Explanation
The head pointer identifies the first node in the linked list.
- A
- 49
What does the next reference of the final linked-list node contain?
- A
null
- B
The list length
- C
The previous node
- D
The head node
Correct answer
Anull
Explanation
The final node has no successor, so its next reference is null.
- A
- 50
What is a major advantage of linked lists over fixed-size arrays?
- A
Linked lists always use less memory than arrays
- B
Linked lists can grow dynamically
- C
Linked lists provide instant random access
- D
Linked lists automatically keep values sorted
Correct answer
BLinked lists can grow dynamically
Explanation
Linked lists can expand by connecting additional nodes rather than requiring a predetermined array capacity.
- A
- 51
What is a major advantage of arrays over linked lists?
- A
Arrays provide direct random access by index
- B
Arrays make insertion at the beginning constant-time
- C
Arrays always have dynamic size
- D
Arrays require no memory allocation
Correct answer
AArrays provide direct random access by index
Explanation
An array can retrieve an element at a known index directly, whereas a linked list must be traversed node by node.
- A
- 52
Why can linked-list insertion and deletion be efficient after the relevant location is found?
- A
All elements are automatically copied into a new array
- B
The values are sorted before every operation
- C
Only node references need to be changed
- D
The list uses binary search to locate every node
Correct answer
COnly node references need to be changed
Explanation
Insertion and deletion can be performed by redirecting links rather than shifting many elements.
- A
- 53
What does a linked-list index lookup generally require?
- A
Traversing from the head through preceding nodes
- B
Hashing the requested index
- C
Checking only the final node
- D
Sorting the nodes by value
Correct answer
ATraversing from the head through preceding nodes
Explanation
Because linked lists do not support direct indexing, the list must be traversed from the head.
- A
- 54
What happens when the head node is removed from a linked list?
- A
The head remains unchanged
- B
The head is updated to the removed node's next node
- C
The last node becomes null
- D
Every node is deleted
Correct answer
BThe head is updated to the removed node's next node
Explanation
The next node becomes the new first node when the current head is removed.
- A
- 55
What is a trie primarily designed to store?
- A
Graph edges by their weights
- B
Numerical values in heap order
- C
Queue elements by priority
- D
Strings or words by shared prefixes
Correct answer
DStrings or words by shared prefixes
Explanation
A trie stores words one character at a time, sharing nodes for common prefixes.
- A
- 56
What does each step or node in a trie represent?
- A
A character in a word
- B
A complete graph
- C
A numerical hash bucket
- D
A queue position
Correct answer
AA character in a word
Explanation
Each trie node corresponds to a character along a word path.
- A
- 57
Why must a trie distinguish between a node that ends a word and one that merely continues a prefix?
- A
A prefix can exist without being a complete word
- B
Words cannot share initial characters
- C
Trie nodes cannot contain more than one character
- D
Every prefix is automatically a complete word
Correct answer
AA prefix can exist without being a complete word
Explanation
A marker such as isEnd indicates whether the current character completes a stored word, even if longer words continue from it.
- A
- 58
What is the defining relationship between parent and child nodes in a min-heap?
- A
Each parent must equal both children
- B
Each parent is less than or equal to its children
- C
Each parent is greater than or equal to its children
- D
Each parent must have exactly one child
Correct answer
BEach parent is less than or equal to its children
Explanation
In a min-heap, the smallest value is at the root and every parent is no greater than its children.
- A
- 59
What is the defining relationship between parent and child nodes in a max-heap?
- A
Each parent is greater than or equal to its children
- B
Each parent must have exactly two children
- C
Each parent must be smaller than both children
- D
Each parent is less than or equal to its children
Correct answer
AEach parent is greater than or equal to its children
Explanation
A max-heap places the largest value at the root, with each parent at least as large as its children.
- A
- 60
What does it mean for a binary heap to be complete?
- A
Every node has exactly two children
- B
The values are completely sorted from left to right
- C
Every level is full except possibly the last, which fills left to right
- D
All leaves occur at different depths
Correct answer
CEvery level is full except possibly the last, which fills left to right
Explanation
A complete binary tree fills every level before beginning the next and fills a partially complete final level from left to right.
- A
- 61
Why can a binary heap be efficiently represented as an array?
- A
Its values are always stored in sorted array order
- B
It contains no leaf nodes
- C
Its complete structure allows child and parent indexes to be calculated
- D
It never contains duplicate values
Correct answer
CIts complete structure allows child and parent indexes to be calculated
Explanation
The heap's structure makes parent-child relationships computable from indexes without storing explicit node references.
- A
- 62
Using one-based indexing, what is the index of the left child of a heap element at index i?
- A
2i
- B
i−1
- C
2i+1
- D
⌊i/2⌋
Correct answer
A2i
Explanation
With one-based heap indexing, the left child is at index 2i.
- A
- 63
Using one-based indexing, what is the index of the right child of a heap element at index i?
- A
2i+1
- B
2i
- C
⌊i/2⌋
- D
i+1
Correct answer
A2i+1
Explanation
With one-based heap indexing, the right child is at index 2i+1.
- A
- 64
Using one-based indexing, what is the index of the parent of an element at index i?
- A
2i+1
- B
i−2
- C
⌊i/2⌋
- D
2i
Correct answer
C⌊i/2⌋
Explanation
The parent index is the floor of i/2, meaning the result is rounded down to the nearest integer.
- A
- 65
What happens when a new value is inserted into the described min-heap?
- A
It is placed according to alphabetical order
- B
It is inserted at the root without comparison
- C
It is appended and moved upward while smaller than its parent
- D
It replaces the root and moves downward immediately
Correct answer
CIt is appended and moved upward while smaller than its parent
Explanation
Heap insertion first places the value at the end, then repeatedly swaps it with its parent when it violates the min-heap property.
- A
- 66
What happens when the minimum value is removed from the described min-heap?
- A
The first leaf becomes the new root without further comparisons
- B
All elements are shifted one position left
- C
The heap is converted into a binary search tree
- D
The last element moves to the root and moves downward as needed
Correct answer
DThe last element moves to the root and moves downward as needed
Explanation
After removing the root, the final element is moved to the root and sifted downward to restore heap order.
- A
- 67
What ordering does the described heap sort produce when using a min-heap?
- A
Insertion order
- B
Least to greatest
- C
Greatest to least
- D
Random order
Correct answer
BLeast to greatest
Explanation
Repeatedly removing the minimum from a min-heap produces the values in ascending order.
- A
- 68
What is the average and worst-case time complexity of the described heap sort?
- A
O(n)
- B
O(n2)
- C
O(1)
- D
O(nlogn)
Correct answer
DO(nlogn)
Explanation
The content identifies heap sort as having average and worst-case performance of O(nlogn).
- A
- 69
In graph terminology, what are the data items or points called?
- A
Nodes or vertices
- B
Edges or links
- C
Buckets or slots
- D
Roots or leaves
Correct answer
ANodes or vertices
Explanation
The points in a graph are called nodes or vertices.
- A
- 70
What are the connections between graph nodes called?
- A
Edges
- B
Buckets
- C
Roots
- D
Indexes
Correct answer
AEdges
Explanation
Edges represent relationships or connections between vertices.
- A
- 71
What distinguishes an undirected graph from a directed graph?
- A
Undirected graphs cannot contain cycles, whereas directed graphs can
- B
Undirected graphs have weights, whereas directed graphs never do
- C
Undirected graphs use numbers, whereas directed graphs use strings
- D
Undirected edges have no direction, whereas directed edges do
Correct answer
DUndirected edges have no direction, whereas directed edges do
Explanation
An undirected edge represents a mutual connection, while a directed edge points from one node toward another.
- A
- 72
What does an adjacency list associate with each vertex?
- A
A list of every possible path in the graph
- B
A matrix containing only edge weights
- C
A collection of neighboring vertices
- D
A list of nodes in insertion order
Correct answer
CA collection of neighboring vertices
Explanation
An adjacency list records the vertices directly connected to each vertex.
- A
- 73
What does an adjacency matrix use to represent an edge?
- A
A 1 indicates a relationship and a 0 indicates none
- B
A 0 indicates a relationship and a 1 indicates none
- C
A negative number indicates every relationship
- D
A string indicates a relationship and null indicates none
Correct answer
AA 1 indicates a relationship and a 0 indicates none
Explanation
In the described unweighted adjacency matrix, one represents an edge and zero represents no edge.
- A
- 74
What do the rows and columns represent in an adjacency matrix?
- A
Rows represent nodes and columns represent path lengths
- B
Both rows and columns represent graph nodes
- C
Rows represent edges and columns represent nodes
- D
Rows represent weights and columns represent buckets
Correct answer
BBoth rows and columns represent graph nodes
Explanation
An adjacency matrix uses both dimensions to identify pairs of nodes.
- A
- 75
How does an incidence matrix differ from an adjacency matrix?
- A
Rows represent edges and columns represent nodes only in undirected graphs
- B
Rows represent nodes and columns represent edges
- C
Both rows and columns represent edge weights
- D
Both rows and columns represent nodes
Correct answer
BRows represent nodes and columns represent edges
Explanation
An incidence matrix uses rows for vertices and columns for individual edges.
- A
- 76
How does an incidence matrix represent an undirected edge?
- A
It places a 1 in the rows of the two connected nodes
- B
It places a 1 only in the row of the source node
- C
It places a 0 in both connected rows
- D
It places a negative 1 in both connected rows
Correct answer
AIt places a 1 in the rows of the two connected nodes
Explanation
Each undirected edge connects two nodes, so its column contains a one in both corresponding node rows.
- A
- 77
How are directed edges represented in the described incidence matrix?
- A
Both endpoints receive a −1 regardless of direction
- B
A 1 marks an entering edge and −1 marks a leaving edge
- C
Both endpoints receive a 1 regardless of direction
- D
A 1 marks an edge leaving a node and −1 marks an edge entering it
Correct answer
DA 1 marks an edge leaving a node and −1 marks an edge entering it
Explanation
For directed graphs, the source receives one and the destination receives negative one.
- A
- 78
What does a weighted graph attach to its edges?
- A
A Boolean indicating whether nodes are sorted
- B
Numerical values representing the cost or weight of connections
- C
A unique root node
- D
A stack position
Correct answer
BNumerical values representing the cost or weight of connections
Explanation
Weighted edges carry numerical values whose meaning depends on the application.
- A
- 79
What is the purpose of graph traversal algorithms?
- A
To visit graph nodes and analyze their relationships
- B
To ensure that all node labels are unique
- C
To remove every edge from a graph
- D
To convert every graph into a tree
Correct answer
ATo visit graph nodes and analyze their relationships
Explanation
Traversal algorithms systematically visit nodes to explore a graph or determine relationships such as distances.
- A
- 80
What is the main strategy of breadth-first search?
- A
Visit nodes in descending numerical order
- B
Visit all nodes at the current distance before moving farther away
- C
Visit only nodes with weighted edges
- D
Follow one path as deeply as possible before backtracking
Correct answer
BVisit all nodes at the current distance before moving farther away
Explanation
Breadth-first search explores neighbors first, then neighbors of those nodes, proceeding outward by distance.
- A
- 81
What does the distance value of infinity represent in the described breadth-first search?
- A
The node is exactly one edge away
- B
The node is the root node
- C
The node has the greatest numerical label
- D
The node has not been reached from the start node
Correct answer
DThe node has not been reached from the start node
Explanation
Distances begin at infinity to indicate that a node is currently considered unreachable or unvisited from the selected root.
- A
- 82
What distance is assigned to the starting node in breadth-first search?
- A
−1
- B
Infinity
- C
1
- D
0
Correct answer
D0
Explanation
The root node is zero edges away from itself.
- A
- 83
Why does breadth-first search use a queue?
- A
It processes discovered nodes in order of their distance from the root
- B
It prevents the graph from containing cycles
- C
It stores only nodes that have no neighbors
- D
It guarantees that the largest node is processed first
Correct answer
AIt processes discovered nodes in order of their distance from the root
Explanation
A queue ensures that nodes discovered earlier are processed before nodes discovered later, producing breadth-first order.
- A
- 84
When does the described breadth-first search assign a distance to a neighbor?
- A
Only when the neighbor is the root
- B
Only when the neighbor has no outgoing edges
- C
When the neighbor's distance is still infinity
- D
Whenever the neighbor has already been visited
Correct answer
CWhen the neighbor's distance is still infinity
Explanation
A neighbor receives a distance only if it has not already been assigned one.
- A
- 85
How is the distance of a newly discovered neighbor calculated?
- A
The total number of graph nodes
- B
The neighbor's index plus one
- C
The current node's distance plus one
- D
The current node's distance minus one
Correct answer
CThe current node's distance plus one
Explanation
Moving across one edge increases the current distance by one.
- A
- 86
When does the breadth-first search loop stop?
- A
When the first neighbor is found
- B
When every node has the same distance
- C
When the queue contains no more nodes to process
- D
When the root node is removed from the graph
Correct answer
CWhen the queue contains no more nodes to process
Explanation
The traversal continues while the queue is nonempty and stops after all reachable discovered nodes have been processed.
- A

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