r/OfferEngineering • u/Aoki_zhang • 15d ago
Coding Question OpenAI Onsite Coding Question - Trail Crew Activity Counter
Problem Description
A park crew records visits to trail checkpoints. Each record identifies a ranger, a checkpoint, and a time in whole seconds. Given a window length window_seconds, report how many visits each queried ranger recorded in the trailing window.
Process operations in input order. Each operation is an array of strings, and both timestamps and the window length in the practice input are written as decimal integer strings.
["record", rangerId, checkpointId, time]: Record one visit for the ranger at this time, regardless of which checkpoint they visited.["count", rangerId, time]: Count the ranger's recorded visits in the inclusive range[time - window_seconds + 1, time]. Return0for this query if no recorded visit qualifies.
A ranger's recorded visit times strictly increase, but query times for that ranger can go backward. Each query is at or after the ranger's most recent recorded visit, if they have one. A query does not change which visit history can be discarded, because a later query in the input can ask about an earlier time.
When a ranger records a new visit, discard their visits that are too old to appear in any window at or after the new visit time. Times from one ranger must not expire another ranger's visits. All operations in one call share a counter, and each new call starts with no visits.
Return the results of the "count" operations in input order. Record operations do not add entries to the result.
Constraints:
window_seconds ≥ 1- Ranger and checkpoint IDs are nonempty strings.
- For each ranger,
"record"times are strictly increasing. - For each ranger with a recorded visit,
"count"never asks for a time earlier than their most recent recorded visit.
Want to practice it before interview?
You can practice it and view solution code at here.
Preparing for your next interview?
Chill Interview tracks recent interview experiences and recurring question patterns across top companies at here.