AS
AS
0 Overview
Instructions
Total points: 50
Your written solution may be either handwritten and scanned, or typeset. Either way, you
must produce a PDF that is legible and displays reasonably on a typical PDF reader. This
PDF should be submitted via autolab as WA2. You should view your submission after you
upload it to make sure that it is not corrupted or malformed. Submissions that are rotated,
upside down, or that do not load will not receive credit. Illegible submissions may also lose
credit depending on what can be read. Ensure that your final submission contains all pages.
You are responsible for making sure your submission went through successfully.
Written submissions may be turned in up to one day late for a 50% penalty.
1
CSE250: Data Structures - Fall 2024 Written Assignment #2
Each of the following questions ask about runtimes related to the SortedList class you
defined in PA1. When answering, give the most specific runtime bound you can: If a Big
Theta bound exists, give that. Otherwise, give the tightest upper bound (Big-O) you can.
All bounds should be given in terms of n (The variable, as it appears in each question). In at
most two sentences, explain the runtime you gave. Answers without explanations will
receive no credit.
Question 1 (4 pt)
What is the runtime of SortedList.insert on a list where list.size() == n.
Answer: O(n), because we must iterate through each node from the start to
the insertion point. In the worst case, the insertion point can be at the end of
n elements.
Rubric:
(4 pt) Correct answer with a good explanation that highlights the need to search for
the insertion point one step at a time.
(2 pt) Correct Big-O bound as above, but the explanation is not quite complete.
(1 pt) Gives the 4-point answer but with Big-Θ instead of Big-O.
Question 2 (4 pt)
What is the runtime of the following snippet of code:
1 SortedList < Integer > list = new SortedList < >();
2 for ( int i = 0; i < n ; i ++){
3 list . insert ( i )
4 }
Answer: Depending on whether you start the search from the first node or the
last node: Θ(n2 ) if starting from the front, because all insertions are at the end of
the list and require ni=0 i = O(n2 ) steps. If starting from the end, all insertions
P
will require only a single check (Θ(1)), and there are N insertions.
Rubric:
(4 pt) One of the correct answers above with an explanation that roughly matches.
2
CSE250: Data Structures - Fall 2024 Written Assignment #2
(2 pt) If they gave one of the answers above with a matching explanation, but used
Big-O instead of Big-Θ.
(2 pt) If they gave the correct Big-Θ answer above, but with an incomplete explana-
tion
Question 3 (4 pt)
What is the runtime of the following snippet of code:
1 SortedList < Integer > list = new SortedList < >();
2 LinkedListNode < Integer > hint = list . insert (0);
3 for ( int i = 1; i < n ; i ++){
4 hint = list . insert (i , hint )
5 }
Answer: Θ(n) because the hint given will always be right next to the element
we are about to insert, so the cost of each insertion is Θ(1), and there are n
insertions.
Rubric:
(4 pt) One of the correct answers above with an explanation that roughly matches, and
is either the reverse of the answer for the prior question, or the justification explicitly
indicates that both ends are tested.
(2 pt) If they gave one of the answers above with a matching explanation, but used
Big-O instead of Big-Θ.
(2 pt) If they gave the correct Big-Θ answer above, but with an incomplete explana-
tion
Question 4 (4 pt)
What is the runtime of the following snippet of code:
1 SortedList < Integer > list = new SortedList < >();
2 for ( int i = n ; i >= 0; i - -){
3 hint = list . insert ( i )
4 }
Answer: This question is the reverse of question 2. If inserts start from from
the front (or explicitly check both ends), the total runtime is Θ(n) for n Θ(1)
insertions. If inserts start from the end, the total runtime is Θ(n2 ).
3
CSE250: Data Structures - Fall 2024 Written Assignment #2
Rubric:
(4 pt) One of the correct answers above with an explanation that roughly matches,
and is either the reverse of the answer for question 2, or the justification explicitly
indicates that both ends are tested.
(2 pt) If they gave one of the answers above with a matching explanation, but used
Big-O instead of Big-Θ.
(2 pt) If they gave the correct Big-Θ answer above, but with an incomplete explana-
tion
Question 5 (4 pt)
What is the runtime of the following snippet of code:
1 SortedList < Integer > list = new SortedList < >();
2 Random rand = new Random ();
3 for ( int i = 0; i < n ; i ++){
4 list . insert ( rand . nextInt ( n ));
5 }
Answer: O(n2 ), because a single insert could be as bad as O(n) since we have to
search one node at a time for the insertion point, and we make n calls to insert.
Rubric:
(4 pt) Gives the correct Big-O bound with an explanation that highlights an under-
standing of the fact that a single insertion of a random element may take up to n steps,
so the worst case is n2 .
(2 pt) Gives the correct Big-O bound with an explanation that is not quite complete.
(1 pt) Gives the 4 point answer but with Big-Θ instead of Big-O.
The following questions have to do with the following function, which takes a SortedList
of Integers, and a LinkedListNode and ’increments’ the value of one element stored at
the node to the next higher value. All questions assume that list is a SortedList where
list.size() == n.
4
CSE250: Data Structures - Fall 2024 Written Assignment #2
Question 6 (2 pt)
Give the tight, unqualified upper bound on the runtime (Big-O) of increment on a list
with n elements in it?
Answer: Θ(1), because the insertion point is never more than one position in
the linked list away from the insertion point of the new node.
Rubric:
Question 7 (2 pt)
Give the tight, unqualified upper bound on the runtime (Big-O) of the following code
snippet. Assume that node is an arbitrary node of list.
1 for ( int i = 0; i < n ; i ++){
2 node = list . increment ( node )
3 }
Answer: Θ(n), because increment is constant time, and there are n insertions.
Rubric:
Question 8 (3 pt)
Does your answer to Q.6 change if the SortedList stores one LinkedListNode for each
distinct element of the list, instead of grouping elements with the same value into a single
node? In at most two sentences explain why or why not.
5
CSE250: Data Structures - Fall 2024 Written Assignment #2
Answer: The runtime grows to O(n). The worst possible circumstance is when
there are n linked list nodes with the exact same value and node is the first of
these. The hinted insert may take up to O(n) in this case.
Rubric:
(3 pt) The answer is linear time (Big-O or Big-Θ) and includes a roughly accurate
description.
(2 pt) The answer is linear time (Big-O or Big-Θ) but has no reasonable description.
Question 9 (3 pt)
Does your answer to Q.7 change if the SortedList stores one LinkedListNode for each
distinct element of the list, instead of grouping elements with the same value into a single
node? In at most two sentences explain why or why not.
Answer: The runtime is still O(n). Even though a single insertion may step up
to n elements, over the course of n increment calls, each linked list node will be
stepped over at most once.
Rubric:
(3 pt) The answer is linear time (Big-O or Big-Θ) and includes a roughly accurate
description.
(2 pt) The answer is linear time (Big-O or Big-Θ) but has no reasonable description.
For each of the following growth functions, find specific values for constants c and N0 that
will prove the requested bounds (or show that no such constants exist). Show all work for
your proof. Answers given without valid work will receive no credit.
Question 10 (5 pt)
Let f (n) = 6n + 5n log(n10 ) + 9
6
CSE250: Data Structures - Fall 2024 Written Assignment #2
Answer:
f (n) = 6n + 5n log(n10 ) + 9
= 6n + 5n · 10 log(n) + 9
= 6n + 15n log(n) + 9
Let c = c1 + c2 + c3
Rubric:
(5 pt) Values of c and n0 that make the proof true, including a chain of reasoning
that shows that these values are correct.
(3 pt) A proof that f (n) ̸∈ O(n log(n)) (e.g., by missing the log(n1 0) = 10 log(n)
step).
Question 11 (5 pt)
Let g(n) = 2log(n) + 6n4 + 3n2
Answer: Since the dominant term is O(n4 ), the claim is false, and we want to
disprove it. This means we want to show the reverse: For every constant c, there
is some n > n0 where:
7
CSE250: Data Structures - Fall 2024 Written Assignment #2
c
For any constant c, this will be true for n = 6
+1
Rubric:
(5 pt) The proof correctly identifies a way to find a value of n for any given c.
(3 pt) The answer correctly identifies that the formula is not O(n3 )
Rubric:
8
CSE250: Data Structures - Fall 2024 Written Assignment #2
(10 pt) Values of c and n0 that make the Big-O and Big-Θ proofs true, including a
chain of reasoning that shows that these values are correct.
(6 pt) As above, but missing a proof for one of the two bounds.
9
CSE250: Data Structures - Fall 2024 Written Assignment #2
4 Summations
Pk
1. i=j c = (k − j + 1)c
Pk Pk
2. i=j (cf (i)) =c i=j f (i)
Pk P P
k k
3. i=j (f (i) + g(i)) = i=j f (i) + i=j g(i)
Pk P P
k j−1
4. i=j (f (i)) = i=ℓ (f (i)) − i=ℓ (f (i)) (for any ℓ < j)
Pk
5. f (i) = f (j) + f (j + 1) + . . . + f (k − 1) + f (k)
i=j
Pk P
k
6. i=j f (i) = f (j) + . . . + f (ℓ − 1) + i=ℓ f (i) (for any j < ℓ ≤ k)
Pk P
ℓ
7. i=j f (i) = i=j f (i) + f (ℓ + 1) + . . . + f (k) (for any j ≤ ℓ < k)
Pk k(k+1)
8. i=1 i= 2
Pk
9. i=0 2i = 2k+1 − 1
n! ≤ cs nn
5 Inequalities
1. f (n) ≤ g(n) is true if you can find some h(n) where f (n) ≤ h(n) and h(n) ≤ g(n)
2. f (n) ≤ g(n) is true if you can find some h(n) where f (n) − h(n) ≤ g(n) − h(n)
3. f (n) ≤ g(n) is true if you can find some h(n) ≥ 0 (for all n) such that f (n) · h(n) ≤
g(n) · h(n)
4. Take it as a given that: θ(1) ≤ θ(log(n)) ≤ θ(n) ≤ θ(n2 ) ≤ θ(nk ) (for k > 2) ≤ θ(2n )
6 Logarithms
1. log(na ) = a log(n)
10
CSE250: Data Structures - Fall 2024 Written Assignment #2
logc (n)
4. logb (n) = logc (b)
5. log(2n ) = 2log(n) = n
11