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 stringtype: one ofNormal,Video, orPhotoscore: a numeric ranking score
Construct the final feed with these rules:
- Sort all objects by
scorein descending order. If two objects have the same score, preserve their original relative order. - Take all
Photoobjects in this ranked order and group them into consecutive batches of 3. The final batch may contain 1 or 2 photos. - 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.
- Merge these photo batches with all
NormalandVideoobjects, 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:
N1N2[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:
- Sort all objects by
scorein descending order. If two objects have the same score, preserve their original relative order. - Take only the
Photoobjects from this ranked list and group them into consecutive batches of 3. The last batch may contain 1 or 2 photos. - Each photo batch becomes a single feed item. Its score is the score of its first photo.
- Merge these photo batches with all
NormalandVideoobjects, 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
typeis one ofNormal,Video, orPhoto - Scores are numeric values and may be negative or duplicated
Hints
- First create one stable ranked list by sorting on score descending. Python's sort is stable, which helps with tie-breaking.
- 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.