KatInterview

Group and Rank Feed Items with Photo Bundling

Heaps, Sorting & SearchAuto-graded · 18 test cases
MediumAuto-gradedHeaps, Sorting & Searchsortingarraysbatching

Last updated

Rearrange feed items by sorting on rank score, grouping photos into batches of up to three, and maintaining stable ordering for equal scores.

Given a list of feed elements, construct an ordered feed by applying ranking and photo-grouping rules.

Each input element is represented as a dictionary containing three keys:

  • "id": a string unique to the element
  • "type": a string denoting the category ("Normal", "Video", or "Photo")
  • "score": a numerical ranking score (higher is better)

You must assemble the final output list according to the following procedure:

  1. Initial Ranking: Sort all items in descending order of their "score". If two items share the same score, preserve their relative order from the input array (stable sort).
  2. Photo Grouping: Extract all "Photo" items from the sorted list in their established rank order and group them into consecutive chunks of size up to 3. Each chunk contains 1, 2, or 3 photo dictionaries.
  3. Batch Scoring: A photo batch is treated as a single compound feed entry. Its ranking score is defined as the score of its first (highest-ranked) photo.
  4. Final Merging: Merge the photo batches back with the standalone "Normal" and "Video" items. Order all items by score descending. Ties must be resolved using the original sorted position established in Step 1 (the index of the standalone item or the index of the first photo in a batch).

The final returned list contains the individual dictionary items for "Normal" and "Video" entries, and Python lists of photo dictionaries for photo batch entries.

Constraints

  • $0 \le \text{len(items)} \le 10^5$
  • Every item has valid keys "id" (str), "type" ("Normal", "Video", or "Photo"), and "score" (int or float).

Examples

Example 1

Input: build_ranked_feed([{'id': 'N1', 'type': 'Normal', 'score': 100}, {'id': 'N2', 'type': 'Normal', 'score': 90}, {'id': 'P1', 'type': 'Photo', 'score': 80}, {'id': 'P2', 'type': 'Photo', 'score': 70}, {'id': 'V', 'type': 'Video', 'score': 60}, {'id': 'P3', 'type': 'Photo', 'score': 50}, {'id': 'P4', 'type': 'Photo', 'score': 40}, {'id': 'N3', 'type': 'Normal', 'score': 30}])

Output: [{'id': 'N1', 'type': 'Normal', 'score': 100}, {'id': 'N2', 'type': 'Normal', 'score': 90}, [{'id': 'P1', 'type': 'Photo', 'score': 80}, {'id': 'P2', 'type': 'Photo', 'score': 70}, {'id': 'P3', 'type': 'Photo', 'score': 50}], {'id': 'V', 'type': 'Video', 'score': 60}, [{'id': 'P4', 'type': 'Photo', 'score': 40}], {'id': 'N3', 'type': 'Normal', 'score': 30}]

Example 2

Input: build_ranked_feed([{'id': 'P1', 'type': 'Photo', 'score': 10}, {'id': 'N1', 'type': 'Normal', 'score': 10}, {'id': 'P2', 'type': 'Photo', 'score': 9}, {'id': 'P3', 'type': 'Photo', 'score': 8}, {'id': 'V1', 'type': 'Video', 'score': 8}, {'id': 'P4', 'type': 'Photo', 'score': 7}])

Output: [[{'id': 'P1', 'type': 'Photo', 'score': 10}, {'id': 'P2', 'type': 'Photo', 'score': 9}, {'id': 'P3', 'type': 'Photo', 'score': 8}], {'id': 'N1', 'type': 'Normal', 'score': 10}, {'id': 'V1', 'type': 'Video', 'score': 8}, [{'id': 'P4', 'type': 'Photo', 'score': 7}]]

Hints

Hint 1

Python's native `sort` and `sorted` are stable, meaning ties preserve insertion order naturally.

Hint 2

Collect all photos after the initial sort, slice them into sublists of size 3, and mark the starting index of each batch in the sorted order.

Hint 3

When creating candidate elements for the final list, associate each non-photo item and each photo batch anchor with its rank score and original sorted index.