Algorithms Problem-Solving
- 42Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
Order the functions n, n, and n+n by asymptotic growth, grouping functions that are asymptotically equivalent.
42 questions
- 1
Order the functions n, n, and n+n by asymptotic growth, grouping functions that are asymptotically equivalent.
Model answer
n grows slowest, while n and n+n are asymptotically equivalent.
Explanation
The n term dominates n in the sum, so adding n does not change the sum’s asymptotic growth.
- 2
How do logarithmic powers compare asymptotically with positive polynomial powers?
Model answer
For positive a and b, (logn)a grows strictly more slowly than nb.
Explanation
A logarithmic factor cannot match the growth of any positive polynomial power as n becomes large.
- 3
How does an exponential function with a fixed base greater than 1 compare asymptotically with polynomial growth?
Model answer
The exponential function eventually grows faster than any polynomial.
Explanation
For example, an exponential such as 2n eventually exceeds nb for any fixed positive b.
- 4
State Stirling’s approximation for n! and describe how accurately it approximates factorial growth.
Model answer
Stirling’s approximation is n!∼2πn(en)n; the ratio of the two sides approaches 1 as n grows.
Explanation
The approximation captures factorial growth with a multiplicative error that becomes negligible in the limit.
- 5
What is the asymptotic growth of log(n!)?
Model answer
log(n!) is Θ(nlogn).
Explanation
Taking the logarithm of Stirling’s approximation gives a leading term of nlogn; the other terms grow more slowly.
- 6
What does the binomial coefficient (kn) count, and how can it be expressed using factorials?
Model answer
It counts the ways to choose k objects from n objects, and (kn)=k!(n−k)!n!.
Explanation
The factorial expression counts arrangements of all the objects, then divides out the arrangements within the chosen and unchosen groups.
- 7
Show why (3n) grows asymptotically as n3.
Model answer
(3n)=6n(n−1)(n−2), whose leading term is 6n3, so it is Θ(n3).
Explanation
The constant factor and lower-degree terms do not change the asymptotic growth rate.
- 8
What is the asymptotic growth of the central binomial coefficient (n/2n)?
Model answer
For even n, it is asymptotically proportional to n2n.
Explanation
Applying Stirling’s approximation to ((n/2)!)2n! and simplifying gives this growth rate, up to a constant factor.
- 9
How can a ratio limit be used to compare the asymptotic growth of two positive functions f(n) and g(n)?
Model answer
If f(n)/g(n) tends to infinity, f grows faster; if it tends to zero, g grows faster; if it tends to a positive constant, they are asymptotically equivalent.
Explanation
The ratio measures how large one function is relative to the other as n increases.
- 10
Why might taking the logarithm of a ratio make an asymptotic comparison easier?
Model answer
It turns products and quotients into sums and differences, which can make the dominant growth terms easier to identify.
Explanation
After taking logarithms, slower-growing additive terms can often be separated from the dominant term in the comparison.
- 11
What does it mean for an algorithm or data structure to be treated as a black box?
Model answer
Its implementation is hidden, and analysis or algorithms use only its specified public operations and guarantees.
Explanation
This abstraction allows reasoning about what a component supports without depending on how it is built.
- 12
What defines the sequence interface, independently of its implementation?
Model answer
It stores items in an extrinsic order, so items have positions such as first, tenth, or last that do not depend on the items’ values.
Explanation
An array or a linked list can implement a sequence; the interface is the ordered collection of items, not its storage layout.
- 13
Why are an array and a linked list both possible implementations of a sequence interface?
Model answer
Either can store items in an order and support sequence operations, even though they organize and access the items differently.
Explanation
The interface describes the behavior available to users, while the implementation determines how that behavior is achieved.
- 14
Which four constant-time operations are provided by the sequence black box used in the examples?
Model answer
Insert first, insert last, delete first, and delete last.
Explanation
The algorithms in the examples are constructed using these operations without relying on the structure’s internal representation.
- 15
Why does a delete operation that removes an item from a sequence need to return the deleted item for the described algorithms?
Model answer
The algorithm can store the returned item temporarily and reinsert it at another position.
Explanation
For example, swapping the ends requires retrieving both endpoint items before inserting them in opposite positions.
- 16
When may an algorithm use extra space, and what restriction was imposed on the linked-list reversal problem?
Model answer
Extra space may be used unless the problem restricts it; the reversal problem allowed only a constant amount of additional space and no new list nodes.
Explanation
A space restriction rules out approaches that store a nonconstant number of items outside the given linked list.
- 17
What is the difference between an abstract operation description and an implementation-specific description?
Model answer
An abstract description states what operation to perform using the interface; an implementation-specific description relies on internal details such as array indices or node layout.
Explanation
Using the interface makes an algorithm independent of whether the sequence is implemented by an array, linked list, or another structure.
- 18
How can the first and last items of a sequence be swapped using only constant-time endpoint operations?
Model answer
Delete the first and last items and store them, then insert the former last item at the front and the former first item at the back.
Explanation
The procedure uses four endpoint operations and a constant number of temporary variables.
- 19
What is the running time of swapping the endpoints when each endpoint operation takes constant time?
Model answer
The swap takes constant time.
Explanation
It performs a fixed number of constant-time operations, regardless of the sequence length.
- 20
Define the operation shift_left(D,K) on a sequence.
Model answer
It moves the first K items of D to the back in their original order, making the former Kth item last and the former (K+1)th item first.
Explanation
This is a rotation of the sequence’s first K items to its end, not a reversal of those items.
- 21
How can shift_left(D,K) be implemented iteratively using endpoint operations?
Model answer
Repeat K times: delete the first item and insert that item at the back.
Explanation
Each repetition moves the current first item to the end, and the sequence of repetitions preserves the items’ relative order.
- 22
Describe a recursive formulation of shift_left(D,K).
Model answer
For K=0, do nothing; otherwise, move the first item to the back and recursively call shift_left(D,K−1).
Explanation
Each recursive step performs one part of the requested shift and reduces the number of remaining items to move.
- 23
Why does the recursive shift algorithm terminate, and what is its running time?
Model answer
Each call decreases K by one until the base case is reached, so it terminates; it takes O(K) time.
Explanation
Every non-base call performs a constant number of endpoint operations, and there are at most K such calls.
- 24
What is an invariant-based way to reason about the correctness of the recursive shift algorithm?
Model answer
After moving one item, the remaining task is to shift the remaining K−1 items; if that smaller task is completed correctly, the full shift is correct.
Explanation
The base case changes nothing when no items remain to be shifted, and each recursive step preserves the intended order while reducing the task.
- 25
What is the difference between amortized running time and average-case running time?
Model answer
Amortized time bounds the total cost across a sequence of operations on a data structure; average-case time averages over a distribution of possible inputs.
Explanation
Amortized analysis does not require assigning probabilities to inputs or operations.
- 26
What does it mean for an operation to take amortized constant time?
Model answer
Across m operations, their total running time is O(m), even if some individual operations take longer.
Explanation
The total-cost bound means the average cost per operation over the sequence is constant.
- 27
What performance does a dynamic array provide for indexing and for insertion or removal at the back?
Model answer
Indexing takes worst-case constant time, while insertion and removal at the back take amortized constant time.
Explanation
An individual insertion may trigger a costly resize, but such resizes are infrequent enough for the sequence of operations to have the stated amortized bound.
- 28
Why is insertion or deletion at the front inefficient in a conventional dynamic array?
Model answer
Items may need to be shifted to make room at the front or to close the gap after deletion.
Explanation
The number of items shifted can grow linearly with the array’s length.
- 29
How does a linked list’s performance for endpoint operations and lookup differ from a dynamic array’s?
Model answer
A linked list can support insertion and deletion at both ends in constant time, but finding an item by position can take linear time.
Explanation
Unlike an array, a linked list generally must follow pointers from node to node to reach a particular position.
- 30
How can extra space at both ends of an array support efficient insertion and deletion at either end?
Model answer
Keep unused capacity at both ends and place new items into that slack, resizing only when the available space is exhausted.
Explanation
A linear amount of slack allows many endpoint operations between costly rebuilds, supporting amortized constant-time operations.
- 31
Why should a dynamic sequence shrink its storage after many items have been removed?
Model answer
Shrinking prevents the structure from retaining space far beyond what is needed for its current number of items.
Explanation
Without resizing down, a sequence that once held many items could continue occupying a large amount of memory after most items are deleted.
- 32
Why should a resized array retain extra capacity rather than exactly match the current number of items?
Model answer
Retained slack allows more insertions or deletions before another resize is needed, avoiding frequent costly rebuilds.
Explanation
If every resize left no room for the next operation, alternating insertions and deletions could repeatedly trigger expensive resizing.
- 33
How can two dynamic arrays be used to represent one sequence with efficient operations at both ends?
Model answer
Store one portion in a forward-oriented dynamic array and the other portion in a dynamic array viewed in reverse, with the sequence split between them.
Explanation
The relevant end of each array can then support the operations efficiently, while index arithmetic maps sequence positions to the appropriate array.
- 34
What must a two-array representation do if one of its arrays becomes empty?
Model answer
Rebalance the remaining items by splitting them between two arrays again.
Explanation
Rebalancing restores the representation’s division of the sequence so endpoint operations can continue to use both arrays.
- 35
What operations and performance goals are sought for the two-array representation of a sequence?
Model answer
It aims to retain worst-case constant-time indexing and provide amortized constant-time insertion and deletion at both ends.
Explanation
The design combines dynamic arrays’ fast indexing with endpoint slack and occasional rebuilding to manage operations at either end.
- 36
What information does each node and the overall list store in the singly linked-list representation described?
Model answer
Each node stores an item and a next pointer; the list stores its head pointer and size.
Explanation
Following next pointers from the head traverses the list in sequence order.
- 37
What transformation is required for a singly linked list containing 2n nodes in the reversal problem?
Model answer
Reverse the order of the last n nodes while leaving the first n nodes in their original order.
Explanation
The operation modifies the existing list rather than returning a separately constructed list.
- 38
What are the main stages of reversing the last half of a singly linked list?
Model answer
Find the nth node, reverse the next pointers of nodes n+1 through 2n, then reconnect the two portions and terminate the new tail.
Explanation
Separating the task into locating, reversing, and reconnecting the relevant nodes helps preserve the intended list structure.
- 39
When reversing a node’s next pointer, why must the algorithm save the node it originally pointed to?
Model answer
It must save that next node before overwriting the pointer so it can continue traversing the unreversed portion of the list.
Explanation
Overwriting the pointer without saving its target could lose access to the remaining nodes.
- 40
Which references are needed to reverse a linked-list segment iteratively using constant extra space?
Model answer
The algorithm keeps track of the current node, the previous node, and the next node.
Explanation
These references allow the current node to point backward while traversal continues forward through the saved next reference.
- 41
After reversing the last half of the list, how should it be reconnected to the first half?
Model answer
Set the nth node’s next pointer to the new first node of the reversed segment, and set the reversed segment’s new tail pointer to null.
Explanation
These changes connect the two portions and ensure the list ends at the correct node.
- 42
What are the time and additional-space complexities of reversing the last half of a 2n-node list under the stated restrictions?
Model answer
The algorithm takes O(n) time and uses O(1) additional space.
Explanation
It traverses a linear number of nodes and changes their pointers using only a fixed number of temporary references, without creating new nodes.

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