Skip to main content
All Questions

Integers arrive as '+x' (add one copy of x) and '-x' (remove every copy of x). After each query, count the triples of elements with values v, v+d and v+2d for a given d.

Unlock detailed company stats for this questionUpgrade
Start with an empty collection of integers, in which the same value may appear more than once, and a fixed difference d. Process a list of queries of two kinds: '+x' adds one copy of x, and '-x' removes every copy of x. After each query, count the triples of elements (x, y, z) in the collection with x - y = d and y - z = d, that is, three elements whose values are v, v + d and v + 2d. The position of the elements in the collection does not matter, and equal values are distinct elements, so if those three values occur a, b and c times they contribute a * b * c triples. Return the list of counts, one per query. All values lie between -10^9 and 10^9, a removal always names a value that is present, and every count fits in a 32-bit integer. Example with d = 5: the queries +5, +5, +0, +10, +10, -5 give 0, 0, 0, 2, 4, 0.

Community answers

No answers contributed by the community yet.

Related courses

Course

Behavioral Interviews: A Practical Guide for Engineers

Get offers at the level you deserve. Prep for senior+ behavioral interviews at FAANG and Tier-1 companies. Made with staff and principal engineers from Meta, Netflix, Google, and more. Avoid downleveling, elevate past projects, and stop sounding like a robot.

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.