KatInterview

Collapse Image Bursts and Rank a Content Stream

Arrays & HashingAuto-graded · 13 test cases
MediumAuto-gradedArrays & Hashingsimulationsortingtwo-pointers

Last updated

Tests the ability to merge consecutive same-user image posts into gallery items and then apply a stable descending-timestamp sort to the resulting stream.

Problem Statement

A content-sharing platform displays a stream of posts to its users. When someone uploads several images back-to-back, showing each one as a separate card makes the stream feel overwhelming. To keep things readable, the platform merges any run of consecutive image posts from the same author into a single gallery card before the stream is sent to the client.

You receive the raw list of posts in the order they were ingested. Every post is a dictionary with these fields:

  • id (str): A unique identifier for this post.
  • user_id (str): The identifier of the author.
  • type (str): Either "photo" or "text".
  • timestamp (int): Unix epoch time in seconds.
  • content (str): The body text or the image URL.

What You Must Do

Step 1 — Merge image bursts.

Walk through the ingested list from left to right. Whenever you encounter two or more consecutive posts that are all of type "photo" and all share the same user_id, replace that entire run with one new gallery item:

  • id: "album_" followed by the id of the first post in the run.
  • user_id: the shared author.
  • type: "album".
  • timestamp: the maximum timestamp found anywhere in the run.
  • photos: a list of the content values (image URLs) for every post in the run, kept in their original relative order.

A lone image post that is not adjacent to another image post by the same author must not be wrapped in a gallery — leave it exactly as it was (still a "photo" item with its own content field).

Any "text" post is always passed through unchanged, and it breaks a run: two image posts by the same user with a text post between them are not consecutive and therefore not merged.

Step 2 — Sort the merged stream.

Sort all items produced by Step 1 in descending order of timestamp. When two items carry the same timestamp, preserve the relative order they had after Step 1 (stable sort).

Return the final sorted list.


Constraints

  • 0 <= len(items) <= 100,000
  • 0 <= timestamp <= 2 × 10⁹
  • user_id, id, and content are non-empty strings.
  • type is always "photo" or "text".

Examples

Example 1

Input: process_post_stream([{'id': 'p1', 'user_id': 'alice', 'type': 'photo', 'timestamp': 100, 'content': 'url1.jpg'}, {'id': 'p2', 'user_id': 'alice', 'type': 'photo', 'timestamp': 105, 'content': 'url2.jpg'}, {'id': 't1', 'user_id': 'bob', 'type': 'text', 'timestamp': 110, 'content': 'Hello neighbors!'}, {'id': 'p3', 'user_id': 'alice', 'type': 'photo', 'timestamp': 95, 'content': 'url3.jpg'}])

Output: [{'id': 't1', 'user_id': 'bob', 'type': 'text', 'timestamp': 110, 'content': 'Hello neighbors!'}, {'id': 'album_p1', 'user_id': 'alice', 'type': 'album', 'timestamp': 105, 'photos': ['url1.jpg', 'url2.jpg']}, {'id': 'p3', 'user_id': 'alice', 'type': 'photo', 'timestamp': 95, 'content': 'url3.jpg'}]

Hints

Hint 1

Scan the list with an index variable. When you land on a photo, advance a second pointer forward for as long as the next item is also a photo by the same author. That contiguous slice is your complete burst — decide in one step whether it becomes a gallery or stays a lone photo.

Hint 2

A single photo between two text posts (or at the edge of the list) must not become a gallery. The only trigger for merging is a run of two or more consecutive same-author photos with nothing else in between.

Hint 3

Python's built-in sort is stable, so sorting by a negated timestamp key will put the highest timestamps first while naturally keeping tied items in their Step-1 order. You do not need to add a secondary tie-break key.