Recent Advertisement Click Counts
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon reported this one in September 2026, and the whole question comes down to one data structure: a per-ad queue of timestamps. If you've got an Amazon OA in the next day or two, expect a stream of CLICK and COUNT operations, a sliding time window, and 100000 operations that punish anything quadratic. The window is inclusive at both ends, and that boundary is where people lose points. It's a hash map of queues with lazy eviction. Simple once you see it. If you blank mid-assessment, StealthCoder runs invisibly as a safety net and hands you the structure in real time.
The problem
You receive a finite sequence of advertisement click and count operations. Each operation is one of: CLICK <adId> <timestamp>: record one click for the advertisement. COUNT <adId> <timestamp>: report how many clicks that advertisement received during the latest kMinutes. Timestamps are integer seconds and never decrease. Process operations in input order, including operations with the same timestamp. For a count at time t, include clicks whose timestamps are in the inclusive interval [t - 60 * kMinutes + 1, t]. Return one integer for each COUNT operation, in query order. CLICK operations produce no output. Function countRecentAdClicks(operations: String[], kMinutes: int) → int[] Examples Example 1 operations = ["CLICK shoes 100","CLICK books 120","CLICK shoes 159","COUNT shoes 160","COUNT books 180","COUNT shoes 220"] kMinutes = 1 return = [1,0,0] At time 160, only the shoes click at 159 remains in [101,160]. The books click at 120 is outside [121,180], and both shoes clicks are outside [161,220]. Example 2 operations = ["CLICK ad1 10","CLICK ad1 69","COUNT ad1 69","COUNT ad1 70"] kMinutes = 1 return = [2,1] At time 69, timestamp 10 is exactly the inclusive lower boundary. At time 70, the lower boundary advances to 11, so only the click at 69 remains. Constraints 1 <= operations.length <= 100000. 1 <= kMinutes <= 100000. Every operation is exactly CLICK <adId> <timestamp> or COUNT <adId> <timestamp>, with single spaces between tokens. Each advertisement ID contains 1 to 50 lowercase English letters or digits. 0 <= timestamp <= 10^12, and timestamps are nondecreasing. Use signed 64-bit arithmetic for timestamps and window boundaries.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Keep a hash map from adId to a queue of click timestamps. On CLICK, push the timestamp to that ad's queue. On COUNT, compute lo = t - 60*kMinutes + 1, pop from the front while the front is less than lo, then the answer is the queue size. Timestamps never decrease, so the queue stays sorted and each click is pushed and popped at most once. That's amortized O(1) per operation. The classic pitfall is the off-by-one: the interval is [t - 60k + 1, t], so a click at exactly t - 60k is out. Example 2 tests this. Use 64-bit math since timestamps reach 10^12 and 60*k*... overflow in 32-bit. Parse each operation by splitting on single spaces. A binary search over a per-ad list also works, but the queue is cleaner. StealthCoder is your hedge if the boundary logic slips under pressure.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Recent Advertisement Click Counts cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Recent Advertisement Click Counts FAQ
What's the trick to Recent Advertisement Click Counts?+
Use a hash map from adId to a queue of click timestamps. Timestamps never decrease, so each queue is already sorted. On COUNT, pop expired timestamps from the front, then return the queue size. Every click is added and removed once, so it's linear overall.
How do I get the window boundary right?+
The lower bound is t - 60*kMinutes + 1, inclusive, and the upper bound is t. A click at exactly t - 60*kMinutes is expired. Evict while front < lo. Check it against Example 2, where timestamp 10 counts at time 69 but not at 70.
Do I need binary search instead of a queue?+
No. Binary search on a per-ad array works in O(log n) per COUNT, but the queue gives amortized O(1) and less code. Since timestamps are nondecreasing, front eviction is safe. Pick whichever you can write without bugs.
What edge cases should I test before submitting?+
Test a COUNT for an ad with no clicks, which returns 0 and shouldn't crash on a missing key. Test multiple operations at the same timestamp, processed in input order. Test large timestamps near 10^12 to confirm you're using 64-bit integers.
How do I prepare for this in 48 hours?+
Write the queue-per-key sliding window once from scratch, then rerun both examples by hand. Practice parsing the string operations and the inclusive boundary. It's a pattern like hit counter and logger rate limiter, so one clean pass is enough.