As reported: Uber SDE2 online assessment (India, 2025), 70 minutes, 4 questions; question 4:
Qqueries and an integerlimit; each query is+X(addXto the list) or-X(remove the first occurrence ofX), starting from an empty list; after each query return the number of pairs in the list whose absolute difference equalslimit.
Process queries in order on an initially empty list. A query "+X" adds the value X; a query "-X" removes one occurrence of X (it is always present). After each query, report the number of pairs of positions i < j in the list with |list[i] − list[j]| = limit.
Example
queries = ["+4","+6","+2","+4","-6"], limit = 2
Output: [0,1,2,4,2]
After +4, +6, +2, +4 the list is [4, 6, 2, 4]: the pairs (4,6), (4,2), (6,4), (2,4) differ by 2. Removing 6 leaves [4, 2, 4] with 2 pairs.
Constraints: 1 ≤ Q ≤ 10^5; 1 ≤ X ≤ 10^9; 0 ≤ limit ≤ 10^9. Counts can exceed 32 bits.
Clarifications
- With
limit = 0, every two equal values form a pair.
Code runner format: one JSON value per line: queries, limit. Implement pairCounts(queries, limit); the harness prints the list of counts.