Reported August 2025
Amazonprefix sum

Count Picked Items Less Than Queries

Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Amazon OA. Under 2s to a working solution.
Founder's read

The data structure this Amazon OA question hinges on is a difference array, and it's the whole game. Reported in August 2025, it dresses up as a warehouse story, but it's really range-update counting plus a sorted lookup. Orders pick ranges of indices, the same item can be picked many times, and each query asks how many picked values are strictly below a number. Brute force dies fast. If you know the trick, it's about fifteen lines. If you blank, StealthCoder is the invisible safety net running on your screen during the live OA.

The problem

A warehouse has items represented by an array items, where items[i] is the value of the i-th item.
There are several orders. The i-th order picks every item in the inclusive index range startIndex[i] through endIndex[i]. Across all orders, this creates one combined multiset of picked item values.
For each value query[i], return how many picked items have value strictly less than query[i].

Function
countPickedItemsLessThan(items: int[], startIndex: int[], endIndex: int[], query: int[]) → int[]

Examples
Example 1
items = [1,2,5,4,5]
startIndex = [0,0,1]
endIndex = [1,2,2]
query = [2,4]
return = [2,5]
The orders pick values [1,2], [1,2,5], and [2,5]. Two picked values are less than 2, and five are less than 4.

Constraints
1 <= items.length
startIndex.length == endIndex.length
0 <= startIndex[i] <= endIndex[i] < items.length

Reported by candidates. Source: FastPrep

Pattern and pitfall

Step one: don't expand the orders. Build a difference array over indices. For each order, add 1 at startIndex and subtract 1 at endIndex+1. A prefix sum gives cnt[i], the number of times items[i] was picked. Step two: pair each value with its count, sort by value, and build a prefix sum of counts over the sorted order. Step three: for each query, binary search for the first sorted value that is >= query, then return the prefix count before it. That's strictly less than. The classic pitfall is using upper bound instead of lower bound, which counts equal values. Another is forgetting the endIndex+1 slot, so size the array n+1. Duplicate values are fine, since the sorted prefix handles them. Total cost is O((n + m) log n). If the binary search details slip under pressure, StealthCoder can hand you the clean version during the live OA.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Count Picked Items Less Than Queries cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Amazon's OA.

Amazon reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Count Picked Items Less Than Queries FAQ

How hard is the Amazon Count Picked Items Less Than Queries question really?+

Medium. Two ideas are stacked: a difference array for range counts and a sorted prefix sum with binary search. Each piece is standard. The difficulty is seeing that you never need to build the multiset explicitly. Once you see that, it's quick.

What's the trick to avoid time limit errors?+

Never loop over every order's range. That's O(n times orders). Use a difference array so each order costs O(1), then one prefix sum pass gives how many times each item was picked. Queries then run in O(log n) each.

Should I use lower bound or upper bound for the query?+

Lower bound. You want values strictly less than the query, so find the first sorted value that is >= query and take the prefix count before that index. Upper bound would include items equal to the query and give wrong answers on ties.

How do I handle duplicate item values?+

Sort items by value alongside their pick counts, then build a prefix sum of the counts. Duplicates just sit next to each other and their counts add up. Lower bound lands before the whole group of equal values, so nothing gets double counted.

How do I prepare for this in 48 hours?+

Practice writing a difference array with the n+1 sizing, then a prefix sum, then a lower bound by hand. Run the sample: items [1,2,5,4,5], expect [2,5]. Also test a query smaller than every item and one larger than every item. Those edges catch most bugs.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Amazon.

OA at Amazon?
Invisible during screen share
Get it