AI-Assisted Coding Lab · Page a Feed With Cursors

45:00left of 45:00

Page a Feed With Cursors

The mobile apps page through a home feed of hundreds of millions of posts, and the cursors they hold come back days later. The README has the rules and the operational stakes. Build feed.py with the assistant, and weigh each suggestion against the table's size and the fact that cursors are client input.

feed.py1 of 3

Files

feed.py

Python · Editable
import base64
import json
class InvalidCursor(Exception):
"""The client sent a cursor we didn't issue. Answer 400."""
MAX_LIMIT = 100
def create_schema(conn):
conn.execute("CREATE TABLE posts (id INTEGER PRIMARY KEY, created_at INTEGER NOT NULL, body TEXT NOT NULL)")
def encode_cursor(created_at, post_id):
raise NotImplementedError
def decode_cursor(cursor):
"""Returns (created_at, post_id). Raises InvalidCursor for anything we didn't issue."""
raise NotImplementedError
def list_posts(conn, limit=20, cursor=None):
"""Newest first. Returns (post ids, next_cursor), and next_cursor is None on the last page."""
where, params = "", []
if cursor is not None:
created_at, post_id = decode_cursor(cursor)
raise NotImplementedError
rows = conn.execute(
f"SELECT id, created_at FROM posts {where} ORDER BY created_at DESC, id DESC LIMIT ?",
params + [limit + 1],
).fetchall()
raise NotImplementedError

Test Output

No runs yet.

Assistant

Scripted
  1. Send the first prompt to start.

Next Prompt

Finish list_posts: return the page and the next cursor.