Group Photos and Sort Feed Items
Company: Nextdoor
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
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.
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
- Do not sort first. Grouping depends on consecutive 'Photo' runs in the original order.
- 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.