Data Structures and Algorithms
- 49Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
Why can the way data is arranged in memory affect an algorithm's performance?
Why can the way data is arranged in memory affect an algorithm's performance?
49 questions
- 1
Why can the way data is arranged in memory affect an algorithm's performance?
- A
Memory representation can change how efficiently an algorithm accesses and processes data
- B
Memory representation guarantees that every algorithm runs in constant time
- C
Memory representation determines whether an algorithm produces logically correct results
- D
Memory representation eliminates the need for an algorithm to use instructions
Correct answer
AMemory representation can change how efficiently an algorithm accesses and processes data
Explanation
An algorithm can be correct but inefficient. The underlying representation and layout of data affect the time and space required to execute it.
- A
- 2
What is the best general strategy for searching randomly ordered data when no additional information is available?
- A
Check every second element and return to skipped elements later
- B
Always check the middle element and discard half the data
- C
Check elements one at a time until the desired value is found
- D
Select a random element because it is usually near the target
Correct answer
CCheck elements one at a time until the desired value is found
Explanation
With randomly ordered data, there is no basis for discarding any unchecked elements. A linear search may therefore need to inspect as many as n elements.
- A
- 3
Why does binary search require sorted data?
- A
Sorting tells the algorithm which half cannot contain the target after each comparison
- B
Sorting ensures that every target appears at the middle position
- C
Sorting lets the algorithm inspect every element simultaneously
- D
Sorting reduces the amount of memory required for each element
Correct answer
ASorting tells the algorithm which half cannot contain the target after each comparison
Explanation
Binary search uses the ordering of the data to determine whether the target must lie to the left or right of the inspected middle element.
- A
- 4
What does contiguous storage mean in an array?
- A
Values are copied into a new location after every access
- B
Values are stored only in alphabetical or numerical order
- C
Values are scattered randomly so that no location is reused
- D
Values occupy adjacent memory locations in a predictable sequence
Correct answer
DValues occupy adjacent memory locations in a predictable sequence
Explanation
An array is a contiguous block of memory in which values are stored back-to-back. This predictable layout supports arithmetic indexing.
- A
- 5
Why can a computer access an array element in constant time?
- A
It automatically sorts the array before performing every access
- B
It searches every preceding element before reaching the requested element
- C
It stores a separate copy of every element at the beginning of memory
- D
It calculates the element's address from its index and jumps directly to it
Correct answer
DIt calculates the element's address from its index and jumps directly to it
Explanation
Because array elements are equally spaced, the computer can use arithmetic on an index to calculate an element's memory address and access it directly.
- A
- 6
If an array has seven elements indexed from zero, which index identifies its final element?
- A
Index 8
- B
Index 7
- C
Index 6
- D
Index 5
Correct answer
CIndex 6
Explanation
Zero-based indexing labels seven elements as 0,1,2,3,4,5,6, so the final index is n−1=6.
- A
- 7
When choosing the middle of seven indexed elements, why is index 3 used?
- A
The middle index is always one less than half the array size
- B
7 divided by 2 is 3.5, which is rounded down to 3
- C
7 divided by 2 is 3.5, which is rounded up to 4
- D
Zero-based indexing makes the middle index equal to the array size
Correct answer
B7 divided by 2 is 3.5, which is rounded down to 3
Explanation
The midpoint calculation uses rac{7}{2}=3.5, and rounding down gives index 3.
- A
- 8
What is the main trade-off involved in allocating extra unused array space?
- A
Extra capacity may support growth but can waste memory needed elsewhere
- B
Extra capacity prevents direct indexing but guarantees faster sorting
- C
Extra capacity removes the need to know the data's element type
- D
Extra capacity makes the array noncontiguous but reduces its memory use
Correct answer
AExtra capacity may support growth but can waste memory needed elsewhere
Explanation
Allocating more space than currently needed provides room for future elements, but unused memory cannot be used by other parts of the program.
- A
- 9
What does big O notation describe?
- A
A lower bound that applies only to the algorithm's best case
- B
The exact number of instructions executed by an algorithm
- C
The amount of memory physically installed in a computer
- D
An upper bound on an algorithm's running time or resource use
Correct answer
DAn upper bound on an algorithm's running time or resource use
Explanation
Big O describes an upper bound on growth, commonly used to characterize worst-case running time.
- A
- 10
What does capital omega notation describe?
- A
An upper bound that ignores the algorithm's input size
- B
The number of memory addresses assigned to an array
- C
The exact midpoint used during binary search
- D
A lower bound on an algorithm's running time or resource use
Correct answer
DA lower bound on an algorithm's running time or resource use
Explanation
Capital omega, written a9 or a9(f(n)), expresses a lower bound on how quickly resource requirements grow.
- A
- 11
When can an algorithm be described using capital theta notation?
- A
When its input contains exactly two possible values
- B
When its best-case time is constant and its worst-case time is linear
- C
When it uses an array rather than a linked data structure
- D
When its upper and lower bounds have the same asymptotic growth
Correct answer
DWhen its upper and lower bounds have the same asymptotic growth
Explanation
Theta notation applies when the upper and lower asymptotic bounds coincide, giving a tight bound.
- A
- 12
What is the worst-case running time of linear search through n elements?
- A
O(n)
- B
O(log n)
- C
O(n2)
- D
O(1)
Correct answer
AO(n)
Explanation
If the target is last or absent, linear search may inspect all n elements, so its upper bound is O(n).
- A
- 13
What is the best-case lower bound for linear search when the target is found at the first position?
- A
a9(n^2)
- B
a9(n)
- C
a9(log n)
- D
a9(1)
Correct answer
Da9(1)
Explanation
If the first inspected element is the target, only a constant number of steps is needed, independent of n, giving a9(1).
- A
- 14
Why is logarithmic growth generally more efficient than linear growth for large inputs?
- A
The logarithmic function increases much more slowly as input size grows
- B
The logarithmic function always examines every element in the input
- C
The linear function requires additional memory while the logarithmic function does not
- D
The logarithmic function is exact only when the input contains seven elements
Correct answer
AThe logarithmic function increases much more slowly as input size grows
Explanation
For increasingly large n, log n grows far more slowly than n, so logarithmic algorithms scale better.
- A
- 15
What is the defining operation of bubble sort?
- A
Repeatedly calculate a hash value and place each item in a bucket
- B
Repeatedly divide the data into halves and merge sorted halves
- C
Repeatedly choose the smallest remaining element and place it next
- D
Repeatedly compare adjacent elements and swap them when they are out of order
Correct answer
DRepeatedly compare adjacent elements and swap them when they are out of order
Explanation
Bubble sort compares neighboring elements and swaps out-of-order pairs. Repeated passes move larger elements toward one end.
- A
- 16
Why does bubble sort repeat passes until no swaps occur?
- A
A pass without swaps identifies the smallest element without comparisons
- B
A pass without swaps reallocates the array into a larger memory block
- C
A pass without swaps guarantees that the array contains only unique values
- D
A pass without swaps shows that every adjacent pair is in the correct order
Correct answer
DA pass without swaps shows that every adjacent pair is in the correct order
Explanation
If a complete left-to-right pass makes no swaps, no adjacent inversion remains, so the data is sorted.
- A
- 17
Why does the bubble-sort loop compare indices only through n−2?
- A
Bubble sort always leaves the last two elements unsorted
- B
Zero-based indexing makes the final element have index n−2
- C
Each index is compared with the next index, so the final valid starting index is n−2
- D
The final element is never part of any comparison
Correct answer
CEach index is compared with the next index, so the final valid starting index is n−2
Explanation
The algorithm compares element i with element i+1. Since the last index is n−1, the largest valid i is n−2.
- A
- 18
What is the defining operation of selection sort?
- A
Compare every adjacent pair and swap each pair that is out of order
- B
Divide the input repeatedly and interleave all resulting elements
- C
Find the smallest remaining element and swap it into the next final position
- D
Hash each element and place it according to its first character
Correct answer
CFind the smallest remaining element and swap it into the next final position
Explanation
Selection sort scans the unsorted portion, selects its smallest element, and swaps it with the element at the current position.
- A
- 19
Why does selection sort inspect fewer elements on each successive pass?
- A
Each pass permanently removes the largest element from memory
- B
The array becomes contiguous only after the first pass
- C
The elements already placed at the beginning are excluded from later searches
- D
The algorithm stops comparing whenever two equal elements appear
Correct answer
CThe elements already placed at the beginning are excluded from later searches
Explanation
After the smallest remaining element is placed, that position is sorted and does not need to be examined again.
- A
- 20
What is the typical asymptotic running time of bubble sort and selection sort?
- A
O(1)
- B
O(n2)
- C
O(log n)
- D
O(n)
Correct answer
BO(n2)
Explanation
Both algorithms perform a number of comparisons proportional to roughly n for roughly n passes, producing quadratic growth.
- A
- 21
Why does the comparison count for selection sort simplify to a quadratic order?
- A
The sum contains only constant terms that become larger than n2
- B
The algorithm compares exactly n2 pairs during every single pass
- C
The algorithm performs only one comparison but stores n2 copies of each value
- D
The sum n−1+n−2+⋯+1 equals 2n2−n, whose dominant term is n2
Correct answer
DThe sum n−1+n−2+⋯+1 equals 2n2−n, whose dominant term is n2
Explanation
The comparisons sum to 2n(n−1)=2n2−n. As n grows, the n2 term dominates the linear term.
- A
- 22
What is the central strategy of merge sort?
- A
Compare adjacent elements until the largest element bubbles upward
- B
Store each element according to the first character of its name
- C
Divide the data into halves, sort the halves, and merge them
- D
Select the smallest element from the entire list on every pass
Correct answer
CDivide the data into halves, sort the halves, and merge them
Explanation
Merge sort repeatedly divides the problem, sorts smaller subproblems, and combines the sorted halves.
- A
- 23
Why does merge sort stop dividing when a subproblem contains one element?
- A
A single element cannot be stored in contiguous memory
- B
A single element automatically becomes the largest value
- C
A single element can always be found through binary search
- D
A single element is already sorted and requires no rearrangement
Correct answer
DA single element is already sorted and requires no rearrangement
Explanation
A one-element sequence is trivially sorted. Merge sort then combines these small sorted sequences.
- A
- 24
What is the main resource trade-off of merge sort compared with bubble sort and selection sort?
- A
Merge sort avoids all comparisons but requires more input elements
- B
Merge sort uses additional space to achieve faster running time
- C
Merge sort uses less space but requires quadratic running time
- D
Merge sort cannot operate on arrays because it needs linked nodes
Correct answer
BMerge sort uses additional space to achieve faster running time
Explanation
Merge sort uses temporary storage while combining subarrays, but this additional space helps reduce its time complexity.
- A
- 25
What is the typical running time of merge sort described in the content?
- A
O(logn)
- B
O(1)
- C
O(n2)
- D
O(nlogn)
Correct answer
DO(nlogn)
Explanation
Merge sort divides the data logarithmically many times while processing all n elements at each level, giving O(nlogn).
- A
- 26
What does random access memory make possible?
- A
Access only to bytes that are arranged alphabetically
- B
Automatic sorting of all values stored in the memory module
- C
Direct access to a specified byte in constant time using its address
- D
Unlimited growth of every array without relocation
Correct answer
CDirect access to a specified byte in constant time using its address
Explanation
The term random access refers to the ability to jump directly to a particular memory address rather than proceeding sequentially.
- A
- 27
How are array indices related to memory addresses?
- A
An index can be used with element size and a starting address to calculate a location
- B
An index changes randomly whenever another value is inserted
- C
An index is a copy of the element's binary value stored beside it
- D
An index identifies only the first element because other elements are searched sequentially
Correct answer
AAn index can be used with element size and a starting address to calculate a location
Explanation
Because array elements are equally spaced, arithmetic based on the index can locate any element's address.
- A
- 28
Why does a floating-point value have limited precision when stored using a fixed number of bits?
- A
Floating-point values cannot be represented using binary digits
- B
Only a finite number of bits are available to represent its value
- C
The operating system stores every floating-point value as an integer
- D
Contiguous memory prevents floating-point values from containing decimals
Correct answer
BOnly a finite number of bits are available to represent its value
Explanation
A fixed allocation such as 32 or 64 bits limits how many digits and fractional details can be represented.
- A
- 29
What is the main limitation of trying to append to a full contiguous array?
- A
The computer cannot calculate the address of the final element
- B
Appending automatically destroys the values already in the array
- C
Adjacent memory may already be allocated to another part of the program
- D
The array cannot store values of the same type after initialization
Correct answer
CAdjacent memory may already be allocated to another part of the program
Explanation
If the immediately following memory is occupied, the array cannot grow in place and may need to be moved to a larger contiguous region.
- A
- 30
What is the cost of relocating an array to a larger memory region?
- A
Only the array's first element must be moved because indices are unchanged
- B
The operating system can enlarge the array without touching any data
- C
Every existing element must be copied or moved to the new region
- D
Relocation changes the values but does not require additional time
Correct answer
CEvery existing element must be copied or moved to the new region
Explanation
Moving the array requires transferring all stored values to the new contiguous region, which takes time proportional to the amount of data.
- A
- 31
How does a linked list support growth in nonadjacent memory?
- A
The list sorts values so that their addresses can be inferred from their sizes
- B
Each node is placed next to the previous node without storing any metadata
- C
Each node stores a value and a pointer to the address of the next node
- D
The list reserves all possible future positions before storing its first value
Correct answer
CEach node stores a value and a pointer to the address of the next node
Explanation
Nodes can be scattered in memory, but each node stores the address of the next node, allowing the structure to be traversed.
- A
- 32
What does a null or zero pointer at the end of a linked list indicate?
- A
The current node has two children in a tree
- B
The current node contains the smallest value in the list
- C
The current node must be moved to the beginning of memory
- D
There is no next node after the current node
Correct answer
DThere is no next node after the current node
Explanation
The final node uses a special convention, often a null or all-zero address, to indicate that traversal should stop.
- A
- 33
What is a major disadvantage of linked lists compared with arrays?
- A
They do not support direct random access to an arbitrary position
- B
They cannot grow after their initial allocation
- C
They cannot remove an element without sorting the entire structure
- D
They require all nodes to be stored contiguously
Correct answer
AThey do not support direct random access to an arbitrary position
Explanation
A linked list must usually be traversed from its first node, so reaching a middle or later node can require linear time.
- A
- 34
Why does a linked list require more space than an array storing the same values?
- A
Each node stores the binary representation of every memory address
- B
Each node stores both its data and a pointer to another node
- C
Each node stores a complete duplicate of the entire list
- D
Each node must reserve space for every possible future value
Correct answer
BEach node stores both its data and a pointer to another node
Explanation
Linked-list nodes contain metadata, such as a next-node address, in addition to the data value.
- A
- 35
What operation can remove a middle node from a linked list efficiently once its predecessor is known?
- A
Shift every later node one position toward the beginning
- B
Update the predecessor's pointer to skip over the removed node
- C
Sort all remaining nodes before reconnecting them
- D
Reallocate the entire list into one contiguous memory block
Correct answer
BUpdate the predecessor's pointer to skip over the removed node
Explanation
Removing a node can be done by changing the preceding node's pointer to refer to the removed node's successor.
- A
- 36
What property defines a binary tree?
- A
Each node has no more than two children
- B
Each node stores exactly two values and no pointers
- C
Every node must have two children and two parents
- D
The tree must contain exactly two levels
Correct answer
AEach node has no more than two children
Explanation
A binary tree permits each node to have zero, one, or two children.
- A
- 37
What ordering property defines a binary search tree?
- A
Values are stored according to the order in which they were inserted
- B
Every node is larger than all nodes below it in both subtrees
- C
Values alternate between odd and even levels throughout the tree
- D
Values in the left subtree are smaller and values in the right subtree are larger than the node
Correct answer
DValues in the left subtree are smaller and values in the right subtree are larger than the node
Explanation
For each node in a binary search tree, values reachable in the left subtree are smaller and values in the right subtree are larger.
- A
- 38
Why can a binary search tree support logarithmic-time search when well balanced?
- A
The tree stores every value in a contiguous sequence of addresses
- B
The tree eliminates the need to compare the target with stored values
- C
Each comparison can select one of two subtrees and discard the other
- D
Each node contains a complete index of all values in the tree
Correct answer
CEach comparison can select one of two subtrees and discard the other
Explanation
At each node, the ordering property determines whether to continue left or right, reducing the remaining search region by about half when balanced.
- A
- 39
What can happen to a binary search tree if values are inserted in an unfavorable order?
- A
It can become elongated and behave similarly to a linked list
- B
It automatically becomes a complete tree with equal-height branches
- C
It becomes an array and no longer requires pointers
- D
It loses all stored values because its root must be replaced
Correct answer
AIt can become elongated and behave similarly to a linked list
Explanation
If values repeatedly extend one side of the tree, the structure can become unbalanced and search may degrade to linear time.
- A
- 40
What is the main trade-off associated with tree structures?
- A
They require sorting after every search but never need rebalancing
- B
They use less space than arrays and always provide constant-time access
- C
They eliminate pointer space but require all values to remain contiguous
- D
They use pointer space to support flexible organization and potentially faster searches
Correct answer
DThey use pointer space to support flexible organization and potentially faster searches
Explanation
Tree nodes use additional pointers, but their organization can reduce search time compared with a linked list.
- A
- 41
What is the purpose of a hash function in a hash table?
- A
It sorts every key by comparing it with every other key
- B
It converts a key into an index or bucket location
- C
It stores all keys in one contiguous linked-list node
- D
It converts values into pointers without using any indexes
Correct answer
BIt converts a key into an index or bucket location
Explanation
A hash function maps an input key to an array position, allowing the table to locate the relevant bucket quickly.
- A
- 42
In the example using the first letter of a name, how could the name David be assigned to an array index?
- A
Multiply the length of David by the number of alphabetic letters
- B
Place David at the index equal to the number of letters in the name
- C
Convert D to its character code and subtract the code for A
- D
Sort all names first and use David's position in the sorted list
Correct answer
CConvert D to its character code and subtract the code for A
Explanation
Using character codes, subtracting the code for A from the code for D maps D to the zero-based index 3.
- A
- 43
What is a collision in a hash table?
- A
A key is stored at two different indexes at the same time
- B
The hash function produces no index for a valid key
- C
Different keys produce the same hash-table index
- D
Two arrays contain identical memory addresses by design
Correct answer
CDifferent keys produce the same hash-table index
Explanation
A collision occurs when multiple inputs map to the same bucket or index.
- A
- 44
How can separate chaining handle multiple names that hash to the same bucket?
- A
Discard every entry except the first one inserted into the bucket
- B
Replace the hash table with a sorted array after every collision
- C
Expand the alphabet so that every name receives a unique first letter
- D
Store the colliding entries in a linked structure associated with that bucket
Correct answer
DStore the colliding entries in a linked structure associated with that bucket
Explanation
Separate chaining uses a linked list or similar structure at each bucket to hold multiple entries with the same hash result.
- A
- 45
Why is a hash table only approximately constant time in general?
- A
Hash functions always examine every key before returning an index
- B
Collisions can create bucket lists that require additional searching
- C
Every hash-table operation requires sorting all stored values
- D
Hash tables cannot use array indexing to access buckets
Correct answer
BCollisions can create bucket lists that require additional searching
Explanation
With a good hash function and few collisions, access is near constant time. Long collision chains can make operations slower, potentially linear.
- A
- 46
What is a dictionary in Python conceptually?
- A
A binary search tree restricted to single-character keys
- B
A hash table that associates keys with values
- C
A linked list that permits duplicate keys without values
- D
A sorted array that stores only numerical indexes
Correct answer
BA hash table that associates keys with values
Explanation
A Python dictionary provides an abstraction for mapping keys, such as names, to associated values.
- A
- 47
What is a Python set designed to represent?
- A
A pair of related coordinates such as latitude and longitude
- B
A sequence whose elements must remain contiguous in memory
- C
A mapping in which every key must have two associated values
- D
A collection of values with no duplicates
Correct answer
DA collection of values with no duplicates
Explanation
A set is a collection that contains unique values rather than repeated copies of the same value.
- A
- 48
What is a tuple particularly suitable for representing?
- A
A fixed grouping of related values such as an x,y coordinate
- B
A hash table whose keys are always alphabetic characters
- C
A sequence that must be repeatedly sorted after each insertion
- D
A dynamically growing collection with arbitrary duplicate values
Correct answer
AA fixed grouping of related values such as an x,y coordinate
Explanation
Tuples can represent grouped values, such as (x,y) or latitude and longitude.
- A
- 49
How is a Python list conceptually related to an array?
- A
It stores values only as key-value pairs accessed through hashing
- B
It is a binary search tree whose nodes have at most two children
- C
It prevents direct indexing by storing every element at a random address
- D
It provides array-like indexed storage while supporting dynamic growth and shrinking
Correct answer
DIt provides array-like indexed storage while supporting dynamic growth and shrinking
Explanation
A list provides the array abstraction while managing memory so the collection can change size more dynamically.
- A

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