Skip to content

Instantly share code, notes, and snippets.

@najibghadri
Last active August 28, 2020 12:07
Show Gist options
  • Save najibghadri/05b36ccc1f3c30e30d8e57269bfd979d to your computer and use it in GitHub Desktop.
Save najibghadri/05b36ccc1f3c30e30d8e57269bfd979d to your computer and use it in GitHub Desktop.
# There is a database where we store all presentation meta data.
# In that database there is a table called 'presentation' with fields:
# - id: Long
# - title: Varchar(255)
# - like_count: Long
# - view_count: Long
# - created_at: Date
# - created_by_usr_id: Long
# - is_public: Boolean
#
# We would like to create a new backend endpoint which can list the top 10 most popular presentations with all the data in the 'presentation' table.
# The response should be in descending order of popularity.
# It should be written in Python and using Django, but in this example we need you to implement the core business logic.
# Implement/modify these functions so someone else in your team could wire them into the request handler.
class Presentation(models.Model):
id = models.LongField(...)
title = models.CharField(...)
like_count = models.LongField(...)
view_count = models.LongField(...)
def query_db_presentation(offset, limit):
return Presentation.objects.raw(f'SELECT * FROM presentation OFFSET {offset} LIMIT {limit}'):
def list_top_ten_presentations(comparator):
offset = 0
limit = 100
top = []
while batch = query_db_presentation(offset, limit):
merged = batch + top
top = merged.sort(lambda t: t["view_count"] + 10 * t["like_count"], reverse=True)[0:10]
offset += limit
return top
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment