Count Cheaper Later Offers for Each Position in a Price Feed
Last updated: November 29, 2026
Quick Overview
Given a list of offer prices in arrival order, return a list where entry i is the number of later offers strictly cheaper than offer i.
Amazon
November 29, 20260
0
0 solved
Given a list of offer prices in arrival order, return a list where entry i is the number of later offers strictly cheaper than offer i.
A Hard Amazon onsite question used to see whether you can maintain an ordered set efficiently. It fits price intelligence work where each offer is ranked against those that arrived after it; interviewers expect you to name a Fenwick tree, merge sort, or balanced BST approach.
What the Interviewer Expects
- Explain why the O(n^2) double loop fails for n around 10^5
- Scan right to left and query how many seen values are smaller
- Compress coordinates so a Fenwick tree can index values
- Implement Fenwick update and prefix query correctly
- Mention the merge sort counting 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
- How would you count later offers that are at most 10 percent cheaper?
- How would you adapt this to a sliding window of the next k offers?
- What changes if you need cheaper earlier offers instead?
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 n integers (can repeat, can be negative). Output: list of n counts, counts[i] = number of j > i with prices[j] < prices[i]....
High-Level Design
Process from right to left so that everything already inserted is a later offer. Map each distinct price to a rank 1..m in sorted order. A Fenwick tree over ranks stores how many inserted offers have ...