Reported March 2024
Sentrydesign

Five-Minute Hit Counter

Reported by candidates from Sentry's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Sentry OA. Under 2s to a working solution.
Founder's read

The edge case that breaks a naive hit counter is the window boundary, and Sentry's March 2024 OA puts it right in the examples. A query at timestamp t counts hits from t - 299 through t, inclusive. That's 300 seconds, not 301, and an off-by-one here fails hidden tests while the sample still passes. Timestamps are non-decreasing and there can be 100000 operations, so you need a queue or a circular buffer, not a rescan. This is a design problem wearing a queue costume. If you blank on the eviction condition mid-assessment, StealthCoder can read the problem and hand you a clean solution while you keep your head.

The problem

Process chronological hit and getHits operations. A query returns the number of hits in the inclusive interval from timestamp - 299 through timestamp. Return "null" for a hit and the decimal count for a query.

Function
runHitCounter(operations: String[], timestamps: int[]) → String[]

Examples
Example 1
operations = ["hit","hit","hit","getHits","hit","getHits"]
timestamps = [1,2,3,4,300,301]
return = ["null","null","null","3","null","3"]
At time 301, the hit at time 1 has expired but the other three remain.

Constraints
Operation and timestamp arrays have equal non-zero length.
Timestamps are positive and non-decreasing.
There are at most 100000 operations.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is a queue of timestamps. On hit, append the timestamp. On getHits, pop from the front while the front is less than timestamp - 299, then return the queue size. Because timestamps never decrease, each element is pushed once and popped once, so total work is linear in operations. The pitfall is the boundary. Use front < t - 299 as the eviction test, not front <= t - 300 written sloppily, and not t - 300 as the inclusive start. Check it on the example: at 301, the start is 2, so the hit at 1 goes and 2, 3, 300 stay, giving 3. Also remember the output is strings, with "null" for hits and the count as a decimal string. Duplicate timestamps are legal, so don't use a set. If you freeze during the live OA, StealthCoder is the hedge that surfaces the eviction logic fast.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Five-Minute Hit Counter 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 by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as design hit counter. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Sentry's OA.

Sentry reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Five-Minute Hit Counter FAQ

What's the trick in the Sentry hit counter question?+

Keep a queue of hit timestamps. On each query, drop everything older than timestamp - 299, then return the queue length. Since timestamps only move forward, every hit is added once and removed once. That keeps the whole run linear even at 100000 operations.

How do I avoid the off-by-one on the 300 second window?+

The window is inclusive on both ends: timestamp - 299 through timestamp. A hit is expired only if it's strictly less than timestamp - 299. Test with the sample: at 301 the hit at 1 is gone, the hit at 2 stays. If your answer there is 3, you're right.

Do I need to handle multiple hits at the same timestamp?+

Yes. Nothing says timestamps are unique, only that they're positive and non-decreasing. Store each hit separately in the queue, or use a timestamp and count pair. A set would silently undercount and fail hidden tests.

What should the function actually return?+

An array of strings the same length as the operations. Return "null" for every hit and the count as a decimal string for every getHits. Returning a real null or an integer is an easy way to fail an otherwise correct solution.

How do I prepare for this in 48 hours?+

Write the queue version from scratch once, then trace the sample by hand. Add a test with duplicate timestamps and one where a query lands exactly on the boundary. Optionally try the circular buffer of 300 slots. It takes under an hour and covers the pattern.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Sentry.

OA at Sentry?
Invisible during screen share
Get it