Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
COMP3121/9101 Assignment
Due Friday 3rd March at 5pm Sydney time
In this assignment we review some basic algorithms and data structures, and we apply the divide-
and-conquer paradigm. There are three problems each worth 20 marks, for a total of 60 marks.
Partial credit will be awarded for progress towards a solution. We’ll award one mark for a response
of “one sympathy mark please” for a whole question, but not for parts of a question.
Any requests for clarification of the assignment questions should be submitted using the Ed forum.
We will maintain a FAQ thread for this assignment.
For each question requiring you to design an algorithm, you must justify the correctness of your
algorithm. If a time bound is specified in the question, you also must argue that your algorithm
meets this time bound. The required time bound always applies to the worst case unless otherwise
specified.
You must submit your response to each question as a separate PDF document on Moodle. You
can submit as many times as you like. Only the last submission will be marked.
Your solutions must be typed, not handwritten. We recommend that you use LaTeX, since:
• as a UNSW student, you have a free Professional account on Overleaf, and
• we will release a LaTeX template for each assignment question.
Other typesetting systems that support mathematical notation (such as Microsoft Word) are also
acceptable.
Your assignment submissions must be your own work.
• You may make reference to published course material (e.g. lecture slides, tutorial solutions)
without providing a formal citation. The same applies to material from COMP2521/9024.
• You may make reference to either of the recommended textbooks with a citation in any
format.
• You may reproduce general material from external sources in your own words, along with a
citation in any format. ‘General’ here excludes material directly concerning the assignment
question. For example, you can use material which gives more detail on certain properties of
a data structure, but you cannot use material which directly answers the particular question
asked in the assignment.
• You may discuss the assignment problems privately with other students. If you do so, you
must acknowledge the other students by name and zID in a citation.
• However, you must write your submissions entirely by yourself.
– Do not share your written work with anyone except COMP3121/9101 staff, and do not
store it in a publicly accessible repository.
– The only exception here is UNSW Smarthinking, which is the university’s official writing
support service.
Question 1
You have recently been hired to work for a large social media company. The company has staff
shortages due to a recent firing spree, and you have been tasked with designing the recommendation
algorithm used on the front page! After thinking for several days, you have figured out a way to
measure a user’s popularity, and now need to design an efficient algorithm to calculate it.
For a given user with n posts, you have access to an integer array V = [v1, v2, . . . , vn], where vi
is the number of views the user’s ith post has in thousands. Since these view counts are used to
order posts on a user’s profile page, they are already sorted, in descending order. The popularity
of a user is the largest value of p such that the user has at least p posts that each have at least p
thousand views.
For example, suppose a user has n = 6 posts, and a view count array V = [7, 6, 5, 4, 4, 1]. Then
they have at least 4 posts each with at least 4,000 views (indeed, they have five such posts), so
p = 4 satisfies the criterion. You can confirm that the criterion is also satisfied for p = 3, but it is
not satisfied for p = 5 since there are only three qualifying posts. The user’s popularity index is
in fact 4, because p = 4 is the largest value for which the criterion holds.
1.1 [7 marks] Before you are given full access to the codebase, you need to try and implement
your new popularity measure on top of the existing code. Given the integer n and the integer array
V for a user as well as an integer value p ≤ n, you must determine whether that user’s popularity
is at least p. This snippet of code runs every single time the page gets loaded, so it needs to be
very efficient.
Design an algorithm that runs in O(1) time and checks whether a user’s popularity is at least
p.
1.2 [13 marks] The code you modified was written by a now-fired intern, and would simply try
every value of p until it found one that worked, for every user. Now that the company has grown
to serve millions of users, your boss has demanded that you develop a faster algorithm. Given the
integer n and the integer array V for a user, you must efficiently compute the user’s popularity
index p.
Design an algorithm that runs in O(log n) time and computes p for a given user.