Get Smaller Items
Learn this problemProblem statement
An Amazon fulfillment center receives a large number of orders each day. Each order is associated with a range of prices of items that need to be picked from the warehouse and packed into a box. There are n items in the warehouse, which are represented as an array items[n]. The value of items[i] represents the value of ith item in the warehouse, and subsequently there are m orders. The start_index and end_index for the order are represented in the arrays start[i] and end[i]. Also start[i] and end[i] are 0-index based. For each order, all the items are picked from the inclusive range from start[i] through end[i].
Given array items, start, end, and query. For each query[i], find the count of elements in the range with a value strictly less than query[i].
Function
getSmallerItems(items: int[], start: int[], end: int[], query: int[]) → long[]
Complete the function getSmallerItems in the editor below.
getSmallerItems has the following parameters:
int items[n]: the value of each itemint start[m]: the start index for each orderint end[m]: the end index for each orderint query[q]: query values
Returns
long output[q]: the answer for each query, the number of picked items having a value strictly less than query.
🐳 Manyyy thanks to the GG of Error-Free Excellence 👉 ✨ spike ✨! 🐝
Examples
Example 1
items = [1, 2, 5, 4, 5]start = [0, 0, 1]end = [1, 2, 2]query = [2, 4]return = [2, 5]
Over the 3 orders, the picked items are [1, 2], [1, 2, 5], and [2, 5].
For the first query, 2 picked items have values less than 2.
5 picked items have values less than 4.
Hence the answer is [2, 5].
Example 2
items = [1, 2, 3, 2, 4, 1]start = [2, 0]end = [4, 0]query = [5, 3]return = [4, 2]Constraints
- 1 ≤ n ≤ 10^5
- 1 ≤ items[i] ≤ 10^9, where 0 ≤ i < n
- 0 < m ≤ 10^5
- 0 ≤ start[i] ≤ end[i] < n, where 0 ≤ i < m
- 1 ≤ q ≤ 10^5
- 1 ≤ query[i] ≤ 10^9, where 0 ≤ i < q
More Amazon problems
- Secure Maximum DeliveriesOA · Seen Jul 2026
- Find Median from Data StreamONSITE INTERVIEW · Seen Jul 2026
- Handwritten SigmoidPHONE SCREEN · Seen Jul 2026
- Handwritten SoftmaxPHONE SCREEN · Seen Jul 2026
- Koko Eating BananasONSITE INTERVIEW · Seen Jul 2026
- Loyal Customers Across Two DaysONSITE INTERVIEW · Seen Jul 2026
- Maximum System Memory CapacityOA · Seen Jul 2026
- Package Delivery SystemOA · Seen Jul 2026