cswork / libraryOpen practice workspace

Coding questions · Nextdoor

Build Ranked Feed With Photo Batching

This question evaluates algorithmic reasoning and data manipulation skills, including stable sorting, grouping/batching, and merging of ranked items while preserving ordering invariants.

You are given a list of feed objects. Each object has:

  • id: a unique string
  • type: one of Normal, Video, or Photo
  • score: a numeric ranking score

Construct the final feed with these rules:

  1. Sort all objects by score in descending order. If two objects have the same score, preserve their original relative order.
  2. Take all Photo objects in this ranked order and group them into consecutive batches of 3. The final batch may contain 1 or 2 photos.
  3. Each photo batch becomes a single feed item. The score of a photo batch is the score of its first photo, which is also the highest-scored photo in that batch.
  4. Merge these photo batches with all Normal and Video objects, then produce the final feed in descending score order. If scores are equal, preserve the relative order from the ranked list.

Example:

Input ranked objects:

  • N1(Normal)
  • N2(Normal)
  • P1(Photo)
  • P2(Photo)
  • V(Video)
  • P3(Photo)
  • P4(Photo)
  • N3(Normal)

Output feed:

  • N1
  • N2
  • [P1, P2, P3]
  • V
  • [P4]
  • N3

Implement a function that returns the final feed.

Build Ranked Feed With Photo Batching

You are given a list of feed objects. Each object is a dictionary with keys id, type, and score. The type is one of Normal, Video, or Photo.

Build the final feed using these rules:

  1. Sort all objects by score in descending order. If two objects have the same score, preserve their original relative order.
  2. Take only the Photo objects from this ranked list and group them into consecutive batches of 3. The last batch may contain 1 or 2 photos.
  3. Each photo batch becomes a single feed item. Its score is the score of its first photo.
  4. Merge these photo batches with all Normal and Video objects, then order the result by descending score. If scores are equal, preserve the relative order from the ranked list.

Return the final feed as a list where each Normal or Video item is represented by its id string, and each photo batch is represented by a list of photo id strings.

For tie-breaking after batching, use the position of the batch's first photo in the ranked list.

Constraints

  • 0 <= len(feed_objects) <= 100000
  • Each object has unique id
  • Each type is one of Normal, Video, or Photo
  • Scores are numeric values and may be negative or duplicated

Hints

  1. First create one stable ranked list by sorting on score descending. Python's sort is stable, which helps with tie-breaking.
  2. Treat each photo batch as a synthetic item whose score and position come from its first photo, then merge those synthetic items with non-photo items.