PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates proficiency in list/sequence transformation, detecting consecutive runs, grouping elements, aggregating attributes, and performing stable sorting while preserving relative order.

  • medium
  • Nextdoor
  • Coding & Algorithms
  • Software Engineer

Group Photos and Sort Feed Items

Company: Nextdoor

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given a list of feed objects. Each object has: - `id`: a unique identifier - `type`: one of `Normal`, `Video`, or `Photo` - `score`: a numeric ranking score Transform the feed into display items using these rules: 1. Scan the original list from left to right. 2. Whenever there is a consecutive run of `Photo` objects, group the photos in that run into batches of exactly 3, preserving the original order of the photos inside each batch. 3. Each batch of 3 photos becomes a `PhotoGroup` item. 4. If a consecutive photo run has 1 or 2 leftover photos after grouping batches of 3, leave those photos as individual `Photo` items. 5. `Normal` and `Video` objects remain individual items. 6. The score of a `PhotoGroup` is the maximum score among the photos inside that group. 7. Return all resulting display items sorted by score in descending order. If two resulting items have the same score, keep their relative order from the transformed list before sorting. Design and implement the transformation.

Quick Answer: This question evaluates proficiency in list/sequence transformation, detecting consecutive runs, grouping elements, aggregating attributes, and performing stable sorting while preserving relative order.

You are given a feed represented as a list of objects. Each object is a dictionary with keys: 'id', 'type', and 'score'. Transform the feed into display items using these rules: 1. Scan the original list from left to right. 2. Whenever you encounter a consecutive run of items whose type is 'Photo', group that run into batches of exactly 3 photos. 3. Each full batch of 3 photos becomes one display item of type 'PhotoGroup'. Its 'ids' field contains the 3 photo ids in their original order, and its 'score' is the maximum score among those 3 photos. 4. If a photo run has 1 or 2 leftover photos after taking as many full groups of 3 as possible, keep those leftovers as individual 'Photo' items. 5. Items of type 'Normal' and 'Video' always stay as individual items. 6. After building the transformed list, sort the resulting display items by score in descending order. 7. If two resulting items have the same score, preserve their relative order from the transformed list before sorting. Return the sorted list of display items. Output item format: - Individual item: {'id': ..., 'type': 'Normal' | 'Video' | 'Photo', 'score': ...} - Grouped photo item: {'ids': [..., ..., ...], 'type': 'PhotoGroup', 'score': ...}

Constraints

  • 0 <= len(feed) <= 200000
  • Each item has keys 'id', 'type', and 'score'
  • 'type' is always one of 'Normal', 'Video', or 'Photo'
  • Scores are integers in the range [-10^9, 10^9]
  • All input ids are unique

Examples

Input: ([{'id': 'a', 'type': 'Normal', 'score': 5}, {'id': 'b', 'type': 'Photo', 'score': 7}, {'id': 'c', 'type': 'Photo', 'score': 3}, {'id': 'd', 'type': 'Photo', 'score': 9}, {'id': 'e', 'type': 'Video', 'score': 6}, {'id': 'f', 'type': 'Photo', 'score': 4}, {'id': 'g', 'type': 'Photo', 'score': 8}],)

Expected Output: [{'ids': ['b', 'c', 'd'], 'type': 'PhotoGroup', 'score': 9}, {'id': 'g', 'type': 'Photo', 'score': 8}, {'id': 'e', 'type': 'Video', 'score': 6}, {'id': 'a', 'type': 'Normal', 'score': 5}, {'id': 'f', 'type': 'Photo', 'score': 4}]

Explanation: The run b-c-d becomes one PhotoGroup with score 9. The later run f-g has only 2 photos, so both stay individual. Then all transformed items are sorted by descending score.

Input: ([{'id': 1, 'type': 'Photo', 'score': 5}, {'id': 2, 'type': 'Photo', 'score': 2}, {'id': 3, 'type': 'Photo', 'score': 5}, {'id': 4, 'type': 'Normal', 'score': 5}, {'id': 5, 'type': 'Video', 'score': 7}, {'id': 6, 'type': 'Photo', 'score': 7}, {'id': 7, 'type': 'Photo', 'score': 1}, {'id': 8, 'type': 'Photo', 'score': 6}, {'id': 9, 'type': 'Photo', 'score': 7}],)

Expected Output: [{'id': 5, 'type': 'Video', 'score': 7}, {'ids': [6, 7, 8], 'type': 'PhotoGroup', 'score': 7}, {'id': 9, 'type': 'Photo', 'score': 7}, {'ids': [1, 2, 3], 'type': 'PhotoGroup', 'score': 5}, {'id': 4, 'type': 'Normal', 'score': 5}]

Explanation: The first three photos form a group with score 5. In the second photo run, 6-7-8 form a group and 9 is leftover. Several items tie on score, so their transformed-list order must be preserved.

Input: ([{'id': 'p1', 'type': 'Photo', 'score': 1}, {'id': 'p2', 'type': 'Photo', 'score': 4}, {'id': 'p3', 'type': 'Photo', 'score': 2}, {'id': 'p4', 'type': 'Photo', 'score': 9}, {'id': 'p5', 'type': 'Photo', 'score': 9}],)

Expected Output: [{'id': 'p4', 'type': 'Photo', 'score': 9}, {'id': 'p5', 'type': 'Photo', 'score': 9}, {'ids': ['p1', 'p2', 'p3'], 'type': 'PhotoGroup', 'score': 4}]

Explanation: The first 3 photos become one group. The last 2 photos are leftovers and remain individual. The two 9-score photos keep their relative order after sorting.

Input: ([{'id': 'x', 'type': 'Video', 'score': -1}, {'id': 'y', 'type': 'Photo', 'score': -5}, {'id': 'z', 'type': 'Photo', 'score': -3}, {'id': 'w', 'type': 'Photo', 'score': -4}, {'id': 'u', 'type': 'Normal', 'score': 0}],)

Expected Output: [{'id': 'u', 'type': 'Normal', 'score': 0}, {'id': 'x', 'type': 'Video', 'score': -1}, {'ids': ['y', 'z', 'w'], 'type': 'PhotoGroup', 'score': -3}]

Explanation: Negative scores are allowed. The three photos form one group with max score -3, and then items are sorted descending: 0, -1, -3.

Input: ([],)

Expected Output: []

Explanation: An empty feed produces an empty result.

Hints

  1. Do not sort first. Grouping depends on consecutive 'Photo' runs in the original order.
  2. Process one photo run at a time, emit full groups of 3, then keep 1 or 2 leftovers as individual photos. After that, use a stable sort by score.
Last updated: Jun 6, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

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

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL 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

  • Simulate Transaction Lock Scheduling - Nextdoor (hard)
  • Build Ranked Feed With Photo Batching - Nextdoor (medium)
  • Merge Weekly Time Intervals - Nextdoor (medium)
  • Merge overlapping weekly time intervals - Nextdoor (medium)
  • Write SQL for app metrics - Nextdoor (hard)