0% found this document useful (0 votes)
7 views2 pages

Prac Midterm

Uploaded by

vvp78084
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Prac Midterm

Uploaded by

vvp78084
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

UENG-101 (2024): Practice Midterm Max. Marks: 50. Time: 2 hours.

1. [2 + 2 + 8 + 3 marks] Given a list of integers, our friend has written this Python function
to find an index i that satisfies a certain CONDITION (line numbers shown for clarity):

1: def find_index(data):
2: for i, x in enumerate(data):
3: if i == x: return i
4: return -1

a) If i = find_index(data) and i >= 0, what CONDITION does i satisfy?


b) If len(data) is 𝑛, our friend claims that this function runs in 𝑂(𝑛) time. State the
assumptions under which this claim is true.
c) Our friend is told that data contains distinct integers sorted in ascending order. Our
friend also has a hint to design a faster algorithm: use a modified version of binary
search. Rewrite the Python function using this hint. Be sure to:
• Use meaningful function and parameter names with appropriate type hints.
(Assume that function arguments always respect type hints.)
• Write a docstring that explains the function’s purpose.
• Write one doctest to help clarify that purpose.
d) Explain why the new function is faster.

2. [3 + 4 + 3 + 5 marks] Recall the btree representation of binary trees from Lab 5. For this
recursive Python function, assume that all nodes in the given btree have int data:

1: def mystery(tree: btree) -> int:


2: if not tree: return 0
3: result = mystery(tree[1]) + mystery(tree[2])
4: if tree[0] % 2:
5: result += tree[0]
6: return result

a) Represent this btree pictorially:


[1, [2, [], []], [3, [4, [], []], []]]
b) What does the mystery function return when it is called with this binary tree as the
argument? Use the given line numbers to specify the execution steps.
c) Suggest an appropriate name for this function and explain its purpose.
d) For a binary tree with 𝑛 > 0 nodes, state when the mystery function’s best and worst
cases (in terms of running time) will occur, and state the asymptotic running times in
both these cases.
3. [5 marks] Recall the btree representation of binary trees from Lab 5. Assume that the
argument to this recursive Python function is a valid btree with distinct int values at
each node:

1: def is_bst(tree: btree) -> bool:


2: if not tree: return True
3: if tree[1] and (tree[1][0] > tree[0]):
4: return False
5: if tree[2] and (tree[2][0] < tree[0]):
6: return False
7: return is_bst(tree[1]) and is_bst(tree[2])

This function is supposed to check if the given tree is actually a binary search tree (BST).
Show that the function is buggy by providing an input argument (tree: btree) as well
as the expected result and the function’s return value for this input argument.

4. [7 + 3 marks] Our friend claims to have written a function to order a list of distinct integers
in such a manner that when these ordered integers are inserted into an initially empty BST
(using the simple insertion algorithm discussed in class), the resulting BST is complete. For
example, given the list [10, 20, 30, 40], our friend’s algorithm:
• should return a valid ordering e.g., [30, 40, 20, 10], but
• should not return an invalid ordering e.g., [30, 40, 10, 20]

Describe an algorithm to determine if the ordering returned by our friend’s algorithm is


valid and analyse your algorithm’s worst-case time complexity. For full credit, your
algorithm should be as efficient as possible. If the ordered list has length 𝑛, assume that
comparing two integers takes 𝑂(log 𝑛) time.

5. [3 + 2 marks] Consider the following AI-generated suggestion (Line 3) for this Python
function:

1: def even_ints(data: list) -> list[int]:


2: """Find all even integers in data."""
3: return [x for x in data if x % 2 == 0]

a) Assume that the function argument respects the given type-hint. State one additional
assumption that is being made by the AI-generated code.
b) To see if this additional assumption can be made, ask an appropriate clarifying question
of the form: What should this function return when data = …?

You might also like