PracHub
QuestionsLearningGuidesInterview Prep

Quick Overview

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

  • medium
  • Nextdoor
  • Coding & Algorithms
  • Machine Learning Engineer

Build Ranked Feed With Photo Batching

Company: Nextdoor

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

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.

Quick Answer: 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 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

Examples

Input: ([{'id': 'N1', 'type': 'Normal', 'score': 100}, {'id': 'N2', 'type': 'Normal', 'score': 95}, {'id': 'P1', 'type': 'Photo', 'score': 90}, {'id': 'P2', 'type': 'Photo', 'score': 89}, {'id': 'V', 'type': 'Video', 'score': 88}, {'id': 'P3', 'type': 'Photo', 'score': 87}, {'id': 'P4', 'type': 'Photo', 'score': 80}, {'id': 'N3', 'type': 'Normal', 'score': 70}],)

Expected Output: ['N1', 'N2', ['P1', 'P2', 'P3'], 'V', ['P4'], 'N3']

Explanation: After ranking, photos are P1, P2, P3, P4. They become batches [P1, P2, P3] with score 90 and [P4] with score 80, then those batches are merged with non-photo items by score.

Input: ([{'id': 'P1', 'type': 'Photo', 'score': 50}, {'id': 'N1', 'type': 'Normal', 'score': 50}, {'id': 'P2', 'type': 'Photo', 'score': 50}, {'id': 'V1', 'type': 'Video', 'score': 40}, {'id': 'P3', 'type': 'Photo', 'score': 30}],)

Expected Output: [['P1', 'P2', 'P3'], 'N1', 'V1']

Explanation: The stable ranked order keeps P1 before N1 before P2 among score-50 items. The batch [P1, P2, P3] gets score 50 and inherits P1's earlier ranked position, so it appears before N1.

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.
Last updated: Apr 19, 2026

Loading coding console...

PracHub

Master your tech interviews with 9,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Group Photos and Sort Feed Items - Nextdoor (medium)
  • Simulate Transaction Lock Scheduling - Nextdoor (hard)
  • Merge Weekly Time Intervals - Nextdoor (medium)
  • Write SQL for app metrics - Nextdoor (hard)