Build Ranked Feed With Photo Batching
NextdoorMachine Learning Engineer · Technical ScreenLast 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 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.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.