Algorithms Problem-Solving
- 51Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
- spaceFlip card
- KI knew it
- DI didn't know it
- ←Previous card
- →Next card
How do n, n, and n+n compare asymptotically?
n grows slowest; n and n+n are asymptotically equivalent, both growing as Θ(n).
Or swipe the card
51 cards
How do n, n, and n+n compare asymptotically?
n grows slowest; n and n+n are asymptotically equivalent, both growing as Θ(n).
How does a logarithmic power compare with a positive polynomial power?
For positive constants a and b, (logn)a=o(nb); the logarithmic power grows strictly more slowly.
How does an exponential function compare with a polynomial as n grows?
For fixed c>1 and fixed polynomial degree, cn eventually grows faster than any polynomial in n.
How does nn compare with cn for a fixed constant c>1?
nn grows faster: cnnn=(n/c)n becomes arbitrarily large as n increases.
What does f(n)=o(g(n)) mean?
f grows strictly more slowly than g: for every constant c>0, f(n)<cg(n) for sufficiently large n.
What does it mean to group functions together when ordering by asymptotic growth?
Functions in the same group are asymptotically equivalent: they have the same growth rate up to constant factors.
How can a limit of the ratio f(n)/g(n) reveal which function grows faster?
If the ratio 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.
Why might you take the logarithm of a ratio when comparing two functions?
Taking the logarithm can make the limit easier to evaluate by turning products into sums and powers into factors.
What does the binomial coefficient (kn) count?
The number of ways to choose k objects from a set of n objects, without regard to order.
What is the factorial formula for a binomial coefficient?
(kn)=k!(n−k)!n!.
Why does the binomial coefficient formula divide n! by k!(n−k)!?
n! counts arrangements of all n objects. Dividing by the arrangements within the chosen k objects and the unchosen n−k objects removes orderings that represent the same selection.
What is the asymptotic growth of (3n)?
(3n)=6n(n−1)(n−2)=Θ(n3).
What is Stirling’s approximation for n!?
n!∼2πn(en)n as n→∞.
What is the asymptotic growth of log(n!)?
log(n!)=Θ(nlogn), as follows by taking the logarithm of Stirling’s approximation.
What is the asymptotic growth of the central binomial coefficient (n/2n)?
For even n, (n/2n)=Θ(n2n).
How does (n/2n) compare asymptotically with 2n?
It grows more slowly by a factor of n: (n/2n)=Θ(2n/n).
What is a sequence interface?
An abstract way to store items in an extrinsic order, so items can be identified as the first, a particular position, or the last, regardless of their values.
How does an interface differ from its implementation?
An interface specifies what operations and behavior are available; an implementation specifies how they are achieved. For example, arrays and linked lists can both implement a sequence interface.
What four end operations does the sequence structure in the problem support?
Insert first, insert last, delete first, and delete last.
What running time is assumed for each supported end operation on the abstract sequence?
Each insert-first, insert-last, delete-first, and delete-last operation takes constant time.
What does it mean to use a data structure as a black box?
Use its specified public operations and guarantees without relying on or needing to know its internal implementation.
How can you swap the first and last items using only constant-time end operations?
Delete and save the first item, delete and save the last item, insert the former last item at the front, then insert the former first item at the back.
What is the running time of swapping the ends using the four end operations?
Θ(1), because the algorithm performs a constant number of constant-time operations.
What value does a delete operation return in the described sequence interface?
It returns the item that was deleted.
What does the operation shift_left(D,k) do?
It moves the first k items of sequence D to the back, preserving their order and the order of the remaining items.
What is the recursive step for shifting the first k items to the back?
Delete the first item, insert it at the back, then recursively shift the first k−1 items.
What is a natural base case for the recursive shift operation?
If k=0, do nothing and return.
What should a shift algorithm do when k is outside the permitted range?
It can return without changing the sequence when k<1 or k>∣D∣−1, as in the described algorithm.
Why does the recursive shift algorithm terminate?
Each recursive call reduces k by one, so the calls eventually reach the base case.
What is the running time of shifting k items with the recursive algorithm?
Θ(k): each call does constant work, and there are k calls up to a constant difference.
Why can recursion make an algorithm easier to reason about?
It can reduce a problem to a smaller instance, allowing correctness to be argued using a base case and a simple step that preserves an invariant.
What is amortized running time?
It is a bound on the total cost of a sequence of operations. If an operation is amortized O(1), then m such operations take O(m) total time, even if some individual operations are expensive.
How does amortized analysis differ from average-case analysis?
Amortized analysis bounds the total cost across a sequence of operations on a data structure; average-case analysis averages over possible inputs, usually using an input distribution.
What operations does a dynamic array support efficiently in the described model?
Indexing takes worst-case constant time, while insertion and removal at the back take amortized constant time.
Why is insertion or deletion at the front inefficient in a standard dynamic array?
The other items generally have to be shifted to make room or close the gap, requiring linear time.
How do linked lists compare with arrays for end operations and indexing?
A linked list can support insertion and deletion at both ends in constant time, but finding an item by position can take linear time. An array supports constant-time indexing.
Why can a linked list require linear time to find an item by position?
Nodes may be stored in different memory locations, so the list must follow pointers one by one from a known node until it reaches the target.
How does reserving extra capacity make dynamic-array insertions efficient?
Insertions use the reserved space without reallocating. A resize is needed only after many insertions, allowing its cost to be spread across those operations.
Why should a dynamic array shrink when it becomes sparsely occupied?
Shrinking prevents the structure from retaining an amount of allocated space far larger than the number of stored items.
Why should a dynamic array retain extra capacity after shrinking?
If it shrank to exactly its current number of items, a nearby insertion could immediately force it to grow again. Retaining spare capacity helps avoid repeated resizing.
How can a dynamic array be adapted to support efficient operations at both ends?
Keep a linear amount of spare capacity at both the front and back, and resize or rebuild only after a linear number of end operations.
How can two ordinary dynamic arrays simulate a sequence with efficient operations at both ends?
Store roughly half the sequence in each array, with one array representing one side in reverse. Perform end operations at the appropriate ends of the two arrays.
What must a two-array implementation do if one of its arrays becomes empty?
Rebalance or rebuild by splitting the remaining items between two arrays, restoring the invariant that both sides can support end operations.
What is the structure of a singly linked list node?
Each node stores an item and a pointer to the next node. The list itself can store a head pointer and its size.
What is the goal when reversing the last n nodes of a singly linked list containing 2n nodes?
Keep the first n nodes in their original order and reverse the links among the last n nodes, modifying the existing list.
How can you locate the nth node in a singly linked list when its head is known?
Start at the head and follow the next pointer n−1 times.
Why must a pointer-reversal algorithm save the current node’s next pointer before relinking it?
Relinking overwrites the original next pointer. Saving it first preserves access to the rest of the list.
What references are useful to maintain while reversing a singly linked list segment?
Keep track of the current node, the previous node, and the current node’s original next node. These references allow the link to be reversed without losing access to the remaining nodes.
After reversing the final n nodes, how should the two parts be connected?
Set the nth node’s next pointer to the new head of the reversed segment, and set the old head of that segment’s next pointer to null.
What time and extra-space bounds can be achieved when reversing the last half of a singly linked list in place?
The reversal takes O(n) time and uses O(1) auxiliary space by changing pointers in the existing nodes.
Why can temporarily disconnecting part of a linked list during reversal be safe?
It is safe if the algorithm retains the necessary node references and reconnects the parts before finishing; otherwise, nodes could become inaccessible.

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