Image Carousel Backend with Threaded Comments and Upvotes
Context
Design and implement the backend for an image carousel feature in a web app. Users can view images, add threaded comments per image, and upvote both images and comments. Use in-memory data structures for an efficient interview-ready solution. Assume a single-process service with concurrent requests (e.g., async HTTP server). Authentication exists and provides a stable userId.
Requirements
-
Data modeling for:
-
Images
-
Comments (threaded: parent/child)
-
Upvotes (for both images and comments)
-
RESTful endpoints to:
-
Post a comment (top-level or reply)
-
Upvote an image or a comment (idempotent)
-
Remove an upvote (idempotent)
-
Fetch the carousel with aggregated counts and paginated comments
-
Behavior and guarantees:
-
Idempotency: repeated upvote/un-upvote requests must not double-count and should be safe to retry
-
Concurrency-safety for upvotes and comment creation
-
Pagination for images (carousel) and comments (paginate top-level comments per image)
-
Sorting by time or score (score = upvote count; tie-breaker by time)
-
Basic rate limiting per user/IP
-
Deliverables:
-
Define data models
-
Specify REST endpoints with request/response shape
-
Choose in-memory data structures
-
Provide core functions (pseudo-code is fine) ensuring idempotency and concurrency-safety
-
Explain time and space complexity
Assumptions
-
Single-process node handling concurrent requests; use per-key mutexes for critical sections
-
Time fields are ISO timestamps; IDs are UUIDs
-
Comments: paginate top-level comments only; return full nested replies for each top-level comment page
-
Sorting options: time (newest first) or score (upvotes desc; break ties by newest)
-
Rate limit: example 60 requests/min per userId (fallback to IP if unauthenticated)