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
Coding & Algorithms
Machine Learning Engineer
Meta
November 29, 2026
Machine Learning Engineer
Onsite
Coding & Algorithms
Hard

0

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
Fenwick tree
coordinate compression
ordered statistics
merge sort
inversions
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample Answer
Requirements
  • Input: list of integer scores (duplicates possible), n up to 10^5.
  • Output: list out with out[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 ...


Submit Your Answer
Markdown supported