cswork / libraryOpen practice workspace

Coding questions · Peregrine

Implement pagination and clump grouping

This question evaluates practical skills in paginated API integration, data aggregation and grouping (clump formation), ID-to-name resolution with caching, error and rate-limit handling, and designing unit tests and complexity analysis within the Data Manipulation (SQL/Python) domain.

You are given two pre-implemented APIs: (1) fetch_activities(page: int) -> { total_pages: int, activities: [ { activity_id: string, type: string, user_id: string, payload: object } ] }; (2) fetch_user_name(user_id: string) -> string. Perform the following:

  • Pagination and print: Call fetch_activities for all pages, obtain the total count, aggregate every activity across pages, and print each activity.
  • Group into clumps: Group activities by (type, user_name). Define a class Clump { type: string, name: string, activities: List[Activity] }. Return a list where each group is represented as a Clump, except when a group would contain exactly one activity—in that case return the single Activity object directly instead of a list/Clump.
  • Replace IDs with names: When printing, show user_name (fetched via fetch_user_name) instead of user_id. Minimize redundant user lookups (e.g., with caching), and handle API errors, rate limits, and timeouts gracefully.
  • Take‑home framing: Briefly describe how you would approach a take‑home variant where you must understand business context first, then aggregate the data, and finally filter results. State assumptions, edge cases, and validation steps.
  • Complexity and tests: Provide time/space complexity and outline unit tests, including pagination boundaries, empty pages, missing users, and mixed single-item vs multi-item clumps.

Paginate Activities with User Names

Return the second page of activities with user names instead of user IDs.

Use activities and users. Sort activities by created_at, then activity_id. With page size 3, page 2 means rows 4 through 6 after sorting.

Return these columns:

  • activity_id
  • type
  • user_name
  • payload
  • created_at, formatted as YYYY-MM-DD HH24:MI:SS

Use LIMIT 3 OFFSET 3.

Table users

columntypeconstraints
user_idINTPRIMARY KEY, NOT NULL
user_nameVARCHAR(100)NOT NULL

Sample data:

user_iduser_name
1Alice
2Bob
3Carol
4Dave

Table activities

columntypeconstraints
activity_idVARCHAR(20)PRIMARY KEY, NOT NULL
typeVARCHAR(50)NOT NULL
user_idINTNOT NULL
payloadTEXT
created_atTIMESTAMPNOT NULL

Sample data:

activity_idcreated_atpayloadtypeuser_id
A12025-05-28 09:00:00Login from weblogin1
A22025-05-29 08:30:00Login from mobilelogin1
A32025-05-29 10:15:00Order #1001purchase1
A42025-05-30 09:45:00Login from weblogin2
A52025-05-30 11:00:00Order #1002purchase2
A62025-05-31 14:20:00Order #1003purchase2
A72025-05-31 16:05:00Left a reviewcomment3

Expected output

Second page of activities ordered by timestamp and id, with each activity joined to its user name.

activity_idcreated_atpayloadtypeuser_name
A42025-05-30 09:45:00Login from webloginBob
A52025-05-30 11:00:00Order #1002purchaseBob
A62025-05-31 14:20:00Order #1003purchaseBob

Hints

  1. Join activities to users on user_id.
  2. Sort before applying LIMIT/OFFSET.
  3. Page 2 with page size 3 is LIMIT 3 OFFSET 3.

Group activities into clumps by type and user name

Using the same activities and users tables, group activities by (type, user_name). Define a "clump" as a group with more than one activity for the same (type, user_name). Groups with exactly one activity are considered singletons.

Write a query that returns one row per (type, user_name) group with the following columns:

  • user_name
  • type
  • is_clump (TRUE if the group has more than one activity, FALSE otherwise)
  • activity_ids: a comma-separated list of activity_id values in that group, ordered by activity_id
  • activity_count: the number of activities in the group

Order the result by user_name, then type. Use the provided sample data to determine what the output should look like.

Table users

columntypeconstraints
user_idINTPRIMARY KEY, NOT NULL
user_nameVARCHAR(100)NOT NULL

Sample data:

user_iduser_name
1Alice
2Bob
3Carol
4Dave

Table activities

columntypeconstraints
activity_idVARCHAR(20)PRIMARY KEY, NOT NULL
typeVARCHAR(50)NOT NULL
user_idINTNOT NULL
payloadTEXT
created_atTIMESTAMPNOT NULL

Sample data:

activity_idcreated_atpayloadtypeuser_id
A12025-05-28 09:00:00Login from weblogin1
A22025-05-29 08:30:00Login from mobilelogin1
A32025-05-29 10:15:00Order #1001purchase1
A42025-05-30 09:45:00Login from weblogin2
A52025-05-30 11:00:00Order #1002purchase2
A62025-05-31 14:20:00Order #1003purchase2
A72025-05-31 16:05:00Left a reviewcomment3

Expected output

One row per (type, user_name) group. Groups with more than one activity are marked as clumps. With the sample data, ('login','Alice') and ('purchase','Bob') are clumps; the others are singletons.

activity_countactivity_idsis_clumptypeuser_name
2A1, A2trueloginAlice
1A3falsepurchaseAlice
1A4falseloginBob
2A5, A6truepurchaseBob
1A7falsecommentCarol

Hints

  1. First join activities to users so you can group by user_name and type.
  2. Use COUNT(*) to detect clumps and a string aggregation function (such as STRING_AGG) to combine activity_ids.