1. Home
  2. Explore
  3. Computer Science
  4. Algorithms Problem-Solving

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Algorithms Problem-Solving

  • 42Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Asymptotic Analysis

Order the functions n\sqrt{n}n​, nnn, and n+nn+\sqrt{n}n+n​ by asymptotic growth, grouping functions that are asymptotically equivalent.

42 questions

  1. 1

    Order the functions n\sqrt{n}n​, nnn, and n+nn+\sqrt{n}n+n​ by asymptotic growth, grouping functions that are asymptotically equivalent.

    Model answer

    n\sqrt{n}n​ grows slowest, while nnn and n+nn+\sqrt{n}n+n​ are asymptotically equivalent.

    Explanation

    The nnn term dominates n\sqrt{n}n​ in the sum, so adding n\sqrt{n}n​ does not change the sum’s asymptotic growth.

  2. 2

    How do logarithmic powers compare asymptotically with positive polynomial powers?

    Model answer

    For positive aaa and bbb, (log⁡n)a(\log n)^a(logn)a grows strictly more slowly than nbn^bnb.

    Explanation

    A logarithmic factor cannot match the growth of any positive polynomial power as nnn becomes large.

  3. 3

    How does an exponential function with a fixed base greater than 111 compare asymptotically with polynomial growth?

    Model answer

    The exponential function eventually grows faster than any polynomial.

    Explanation

    For example, an exponential such as 2n2^n2n eventually exceeds nbn^bnb for any fixed positive bbb.

  4. 4

    State Stirling’s approximation for n!n!n! and describe how accurately it approximates factorial growth.

    Model answer

    Stirling’s approximation is n!∼2πn(ne)nn! \sim \sqrt{2\pi n}\left(\frac{n}{e}\right)^nn!∼2πn​(en​)n; the ratio of the two sides approaches 111 as nnn grows.

    Explanation

    The approximation captures factorial growth with a multiplicative error that becomes negligible in the limit.

  5. 5

    What is the asymptotic growth of log⁡(n!)\log(n!)log(n!)?

    Model answer

    log⁡(n!)\log(n!)log(n!) is Θ(nlog⁡n)\Theta(n\log n)Θ(nlogn).

    Explanation

    Taking the logarithm of Stirling’s approximation gives a leading term of nlog⁡nn\log nnlogn; the other terms grow more slowly.

  6. 6

    What does the binomial coefficient (nk)\binom{n}{k}(kn​) count, and how can it be expressed using factorials?

    Model answer

    It counts the ways to choose kkk objects from nnn objects, and (nk)=n!k!(n−k)!\binom{n}{k}=\frac{n!}{k!(n-k)!}(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. 7

    Show why (n3)\binom{n}{3}(3n​) grows asymptotically as n3n^3n3.

    Model answer

    (n3)=n(n−1)(n−2)6\binom{n}{3}=\frac{n(n-1)(n-2)}{6}(3n​)=6n(n−1)(n−2)​, whose leading term is n36\frac{n^3}{6}6n3​, so it is Θ(n3)\Theta(n^3)Θ(n3).

    Explanation

    The constant factor and lower-degree terms do not change the asymptotic growth rate.

  8. 8

    What is the asymptotic growth of the central binomial coefficient (nn/2)\binom{n}{n/2}(n/2n​)?

    Model answer

    For even nnn, it is asymptotically proportional to 2nn\frac{2^n}{\sqrt{n}}n​2n​.

    Explanation

    Applying Stirling’s approximation to n!((n/2)!)2\frac{n!}{((n/2)!)^2}((n/2)!)2n!​ and simplifying gives this growth rate, up to a constant factor.

  9. 9

    How can a ratio limit be used to compare the asymptotic growth of two positive functions f(n)f(n)f(n) and g(n)g(n)g(n)?

    Model answer

    If f(n)/g(n)f(n)/g(n)f(n)/g(n) tends to infinity, fff grows faster; if it tends to zero, ggg 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 nnn increases.

  10. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 20

    Define the operation shift_left(D,K)\text{shift\_left}(D,K)shift_left(D,K) on a sequence.

    Model answer

    It moves the first KKK items of DDD to the back in their original order, making the former KKKth item last and the former (K+1)(K+1)(K+1)th item first.

    Explanation

    This is a rotation of the sequence’s first KKK items to its end, not a reversal of those items.

  21. 21

    How can shift_left(D,K)\text{shift\_left}(D,K)shift_left(D,K) be implemented iteratively using endpoint operations?

    Model answer

    Repeat KKK 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. 22

    Describe a recursive formulation of shift_left(D,K)\text{shift\_left}(D,K)shift_left(D,K).

    Model answer

    For K=0K=0K=0, do nothing; otherwise, move the first item to the back and recursively call shift_left(D,K−1)\text{shift\_left}(D,K-1)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. 23

    Why does the recursive shift algorithm terminate, and what is its running time?

    Model answer

    Each call decreases KKK by one until the base case is reached, so it terminates; it takes O(K)O(K)O(K) time.

    Explanation

    Every non-base call performs a constant number of endpoint operations, and there are at most KKK such calls.

  24. 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−1K-1K−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. 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. 26

    What does it mean for an operation to take amortized constant time?

    Model answer

    Across mmm operations, their total running time is O(m)O(m)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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 37

    What transformation is required for a singly linked list containing 2n2n2n nodes in the reversal problem?

    Model answer

    Reverse the order of the last nnn nodes while leaving the first nnn nodes in their original order.

    Explanation

    The operation modifies the existing list rather than returning a separately constructed list.

  38. 38

    What are the main stages of reversing the last half of a singly linked list?

    Model answer

    Find the nnnth node, reverse the next pointers of nodes n+1n+1n+1 through 2n2n2n, 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. 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. 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. 41

    After reversing the last half of the list, how should it be reconnected to the first half?

    Model answer

    Set the nnnth 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. 42

    What are the time and additional-space complexities of reversing the last half of a 2n2n2n-node list under the stated restrictions?

    Model answer

    The algorithm takes O(n)O(n)O(n) time and uses O(1)O(1)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.

1 of 42
Question 1 of 42

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent