KatInterview

Photo Slideshow with Ranked Comment Threads and Safe Voting

Data Structure DesignModel answer
MediumModel answerData Structure Designdata-structure-designsorted-indexidempotency

Last updated

Design a slideshow widget plus per-photo comment threads with idempotent voting and fast top-k ranking, scaling to 10^5 photos and 10^6 comments.

You are building a photo slideshow widget with a discussion area under each photo. Design and implement the underlying data structures and APIs.

Slideshow behavior

  • Move to the next or previous photo, wrapping around at both ends.
  • Autoplay, advancing at a configurable interval.

Discussion per photo

  • Create, read, edit and delete comments.
  • Users can upvote or downvote a comment.
  • Return the top-k comments for a photo, ordered by score (highest first). Ties are broken by recency (newer first).

Scale

Up to 10^5 photos and 10^6 comments in total. A single photo may be very popular and hold a large share of them.

What to deliver

  1. The data structures you would use, and why.
  2. The time and space complexity of: add comment, delete comment, change vote, top-k query.
  3. The HTTP-style API you would expose, including how comments are paginated.
  4. How you prevent double-counting when a user rapidly toggles their vote (for example up, down, up in quick succession, or a retried request).
  5. Edge cases: empty slideshow, single photo, deleting a comment, k larger than the thread, voting on a missing comment, and large time jumps for autoplay.

You may write code or pseudocode, but the focus is on the design, the invariants and the trade-offs.

Hints

Hint 1

Keep the slideshow and the comment system as independent components. The slideshow is just a cyclic index; think about how to make autoplay testable without threads or sleeping.

Hint 2

Rank comments with an ordered index per photo keyed by (-score, -sequence, id), and a hash map from comment id to the comment. A vote changes the key, so think about what you must remove and re-insert.

Hint 3

Do not store 'upvote' events. Store each user's current vote per comment (-1, 0, +1) and apply only the difference between new and old state; this makes retries and rapid toggles harmless.