As an aspiring developer at Amazon, you are building a prototype for a cart management service.
items, that represents the item ids present in the cart initially.q integers, query, your service must perform as follows.
Complete the function processQueriesOnCart in the editor below.
processQueriesOnCart has the following parameters:
int items[n]: items initially in the cartint query[q]: items to add or remove
Examples
01 · Example 1
items = [1, 2, 1, 2, 1] query = [-1, -1, 3, 4, -3] return = [2, 2, 1, 4]
Initially, there are n = 5 items in the cart represented as cart = [1,2,1,2,1] and queries = [-1,-1,3,4,-3]
| Query | Task | Cart |
|---|---|---|
| -1 | Delete first 1 from cart | [2,1,2,1] |
| -1 | Delete first 1 from cart | [2,2,1] |
| 3 | Append 3 to cart | [2,2,1,3] |
| 4 | Append 4 to cart | [2,2,1,3,4] |
| -3 | Delete first 3 from cart | [2,2,1,4] |
Report [2,2,1,4] as the final cart.
02 · Example 2
items = [5, 1, 2, 2, 4, 6] query = [1, -2, -1, -1] return = [5, 2, 4, 6]
items = [5, 1, 2, 2, 4, 6]
queries = [1, -2, -1, -1]
| Query | Task | Cart |
|---|---|---|
| 1 | Append 1 to cart | [5, 1, 2, 2, 4, 6, 1] |
| -2 | Delete first 2 from cart | [5, 1, 2, 4, 6, 1] |
| -1 | Delete first 1 from cart | [5, 2, 4, 6, 1] |
| -1 | Delete first 1 from cart | [5, 2, 4, 6] |
Report [5, 2, 4, 6] as the final cart.
Constraints
1 <= n, q <= 2 * 10^51 <= items[i] <= 10^9-10^9 <= query[i] <= 10^9- It is guaranteed that
query[i]!= 0
More Amazon problems
- Count Promotional PeriodsOA · Seen Jun 2026
- Find Maximum Total Amount (SDE I, Fungible :)Seen Jun 2026
- Get Minimum AmountOA · Seen Jun 2026
- Find Minimum CostOA · Seen Jun 2026
- Get Smallest Base SegmentOA · Seen Jun 2026
- Select Least Resource TasksOA · Seen Jun 2026
- Product Category Group SizesPHONE SCREEN · Seen May 2026
- Count Connected ComponentsPHONE SCREEN · Seen May 2026
public int[] processQueriesOnCart(int[] items, int[] query) {
// write your code here
}
items[1, 2, 1, 2, 1]
query[-1, -1, 3, 4, -3]
expected[2, 2, 1, 4]
sign in to submit