Count Later Posts With Lower Scores for Every Post in a Feed
Last updated: November 29, 2026
Quick Overview
Given feed posts' quality scores in ranked order, return for each post how many posts ranked after it have a strictly lower score.
Meta
November 29, 20260
0
0 solved
Given feed posts' quality scores in ranked order, return for each post how many posts ranked after it have a strictly lower score.
A Meta onsite hard question, sometimes framed as measuring how far a ranking model is from a score-sorted order. Interviewers accept a merge sort count or a Fenwick tree over compressed ranks, and they push on why a plain BST can degrade.
What the Interviewer Expects
- Reject the O(n^2) double loop with reasoning
- Compress values to ranks
- Sweep right to left with a Fenwick tree of seen ranks
- Query strictly smaller with prefix(rank - 1)
- Mention merge sort counting as an alternative
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Count later posts with a higher score instead.
- Return the total number of inversions as a ranking quality metric.
- How would you compute this for a stream of new posts appended at the end?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: list of integer scores (duplicates possible), n up to 10^5.
- Output: list
outwithout[i] = count of j > i where scores[j] < scores[i]....
High-Level Design
Sweep from the end. When we are at index i, the structure should contain exactly the scores to its right, and we need the count of those strictly below scores[i].
A self-balancing BST with subtree ...