Data Structures & AlgorithmsHardProduct Company SDE · Amazon/Microsoft-style OA

Problem: For a stream of n = 10⁶ (user, timestamp) hits, answer 'how many hits in the last 300 seconds?' after each new hit (timestamps non-decreasing). Which structure gives amortised O(1)?

  1. A. A balanced BST – O(log n) is the best possible
  2. B. A deque of timestamps – push new hits at the back and pop expired ones from the front
  3. C. Recount the whole history each time
  4. D. A stack of timestamps

Correct answer: B. A deque of timestamps – push new hits at the back and pop expired ones from the front

Because timestamps arrive in order, expired hits are always at the front; each hit is pushed once and popped once, so the total work is O(n). A stack exposes the newest hit, not the oldest.

More Coding Interview Problems questions

Exams that ask this topic