Katkat

Build Ranked Feed With Photo Batching

NextdoorMachine Learning Engineer · Technical Screen

Last updated

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.