Data Structures and Algorithms (DSA27) · Week 2

Complexity and the Array · control flow, functions, errors (Lecture 02, Lab 02)

These quizzes are for self-study. Your answers are saved on this device only and are not sent anywhere.

Score: 0 / 44 · Answered: 0

  1. W2-M01 Apply

    f(n) = 3n2 + 5n + 2 is:

  2. W2-M02 Explain

    Which statement is true?

  3. W2-M03 Recall

    f(n) = O(g(n)) means:

  4. W2-M04 Apply

    In the worst case, how many comparisons does linear search make on an array of n elements?

  5. W2-M05 Apply

    The target is present and equally likely to be at any of the n positions. On average, how many comparisons does linear search make?

  6. W2-M06 Apply

    What is the running time of this loop?

    i = n
    while i > 1:
        i = i // 2
  7. W2-M07 Apply

    What is the running time?

    for i in range(n):
        for j in range(i):
            count += 1
  8. W2-M08 Apply

    What is the running time?

    for i in range(n):
        for j in range(5):
            count += 1
  9. W2-M09 Recall

    For a Python list of n items, x in values costs:

  10. W2-M10 Recall

    Which Python list operation is O(1) (amortised)?

  11. W2-M11 Explain

    Why is reading a[i] from an array O(1)?

  12. W2-M12 Apply

    A partly filled array has size 6. How many elements must move to insert a new value at index 2?

  13. W2-M13 Apply

    When inserting into an array, the elements after the gap must be shifted right. In which order?

  14. W2-M14 Apply

    a = Array(4), then a[-1]. What happens?

  15. W2-M15 Apply

    a = Array(5), then a[0] = 7. What is len(a)?

  16. W2-M16 Apply

    A 3 × 4 matrix (3 rows, 4 columns) is stored flat in row-major order. At which index is element (2, 1)?

  17. W2-M17 Apply

    What extra space does reverse_in_place(arr) need (swap from both ends, no second array)?

  18. W2-M18 Explain

    Under the course storage rule (Lecture 02), which of these is not allowed?

  19. W2-M19 Recall

    Python's built-in list is best described as:

  20. W2-M20 Apply

    An O(n) loop is followed by an O(n2) loop. The whole is:

  21. W2-M21 Apply

    values is a Python list of n items. The cost of:

    for i in range(len(values)):
        if values[i] in values[i + 1:]:
            return True
  22. W2-M22 Recall

    When does the else block of a for loop run?

  23. W2-M23 Apply

    Given

    def f(a, L=[]):
        L.append(a)
        return L

    what does f(1) then print(f(2)) print?

  24. W2-M24 Apply

    What is the value of 0 or 'x' and ''?

  25. W2-M25 Apply

    list(range(2, 11, 3)) is:

  26. W2-M26 Apply

    What does print(g()) print?

    def g():
        try:
            return 1
        finally:
            print("bye")
  27. W2-M27 Recall

    In the RAM model of Lecture 02, which of these is not one basic step?

  28. W2-M28 Apply

    An algorithm does n2 basic steps and takes 1 second at n = 10,000. About how long will it take at n = 100,000, on the same machine?

  29. W2-M29 Explain

    Algorithm A does 1000n steps; algorithm B does n2 steps. Which statement is true?

  30. W2-M30 Explain

    Which sentence about linear search is correct?

  31. W2-M31 Apply

    What is the running time of this loop?

    i = 0
    while i * i < n:
        i += 1
  32. W2-M32 Explain

    A recursive total(values) makes one call per element and creates no new data. Its extra space is:

  33. W2-M33 Apply

    A partly filled Array has size 7. How many elements move during remove_at(arr, 7, 2)?

  34. W2-M34 Explain

    remove_at ends with arr[size - 1] = None. Why?

  35. W2-M35 Recall

    In C, after int a[6];, the statement a[6] = 0;:

  36. W2-M36 Explain

    One Python array can hold an int, a str and a list, and still index in O(1). Why?

  37. W2-M37 Recall

    a = Array(3), then a[0:2]. What happens?

  38. W2-M38 Apply

    arr holds [1, 2, 3, 4, 5, 6]. What does it hold after rotate_left(arr, 8)?

  39. W2-M39 Explain

    merge_sorted(a, b) takes the front of a when a[i] <= b[j]. Why <= rather than <?

  40. W2-M40 Apply

    A 3 × 4 matrix holding the values 0 to 11 row by row is stored flat in row-major order. transpose_flat(arr, 3, 4) returns the 4 × 3 transpose, also row-major. At which index of the result is the value 1?

  41. W2-M41 Explain

    "Any sort that works only by comparing elements needs Ω(n log n) comparisons in the worst case." This means:

  42. W2-M42 Apply

    How many pairs does is_sorted(arr) compare when arr holds [5, 1, 2, 3, 4, 6, 7, 8]?

  43. W2-M43 Apply

    What happens when increment() is called?

    count = 0
    
    def increment():
        count = count + 1
  44. W2-M44 Explain

    What does print(users) show?

    users = ['guest1', 'guest2', 'ali', 'guest3']
    for u in users:
        if u.startswith('guest'):
            users.remove(u)
    print(users)

37 marks

Written questions

10 written questions (37 marks). Write your answer first, then open the model answer and its marking points, and mark yourself honestly: each point is worth about one mark.

Written total: 0 / 37

  1. W2-E1 Explain 4 marks

    Big-O throws away constant factors and lower-order terms. Explain why each is thrown away, give one situation where doing so is misleading, and explain how measuring complements counting.

  2. W2-E2 Recall 4 marks

    Define O, Ω and Θ precisely. For f(n) = 3n2 + 2n, give one true statement using each, and one true but uninformative O statement.

  3. W2-E3 Explain 3 marks

    Describe the best, worst and average case of linear search, with the number of comparisons for each. Which one does "linear search is O(n)" usually refer to, and why is that the default?

  4. W2-E4 Explain 4 marks

    Explain why indexing an array is O(1) but inserting into the middle is O(n). What does the fact that an array has a fixed size force you to do when it fills up, and what does that cost?

  5. W2-E5 Explain 4 marks

    State the course storage rule from Lecture 02. Give the reason for it, list what remains allowed, and show how two structures from later in the course follow it.

  6. W2-E6 Recall 3 marks

    Compare Python's list, array.array, numpy.ndarray and a ctypes array on: can it grow, what it holds, and whether it hides costs. Why is the course Array built on ctypes?

  7. W2-E7 Explain 4 marks

    values is a Python list of n numbers. Give the running time of each numbered part of this function, justify each with one of the five rules, and give the total. Why is part (3) not O(n2), although it hides a slice and an in inside a loop?

    def report(values):
        total = 0
        for v in values:                 # (1)
            total += v
        i = len(values)
        while i > 1:                     # (2)
            i //= 2
        for v in values:                 # (3)
            if v in values[:10]:
                total -= v
        return total
  8. W2-E8 Explain 4 marks

    Define the auxiliary space of an algorithm. Give the extra space of reverse_in_place, resized(arr, 2n), merge_sorted(a, b) and a recursive total(values), with a reason for each. Why does recursion use space even when it creates no data, and what limit does Python put on it?

  9. W2-E9 Apply 3 marks

    Explain row-major order. A 5 × 7 matrix (5 rows, 7 columns) is stored flat in row-major order: at which flat index is element (3, 4), and which (row, column) is at flat index 30? Why is reaching (r, c) still O(1)?

  10. W2-E10 Explain 4 marks

    The course Array refuses negative indices, slicing and append. For each refusal, explain why it is deliberate and what you write instead. Then explain the difference between len(a) and the size of a partly filled array, and who keeps track of each.