Shared Counter Snapshot Sum
Updated
Problem statement
Simulate two workers that atomically increment one shared counter, initially 0.
- An event
"THRESHOLD"asks the threshold worker to increment the counter if that worker is still active. - An event
"TIMER"asks the timer worker to increment the counter if that worker is still active. - The one event
"TIMEOUT"represents the timer worker's one-second deadline.
Immediately after any applied increment first makes the counter equal to 10, the threshold worker records a snapshot of 10 and exits. When "TIMEOUT" is processed, the timer worker records the current counter and exits. Events for an exited worker are ignored.
The input sequence contains exactly one timeout and guarantees that both snapshots are recorded before the sequence ends. Return the sum of the two snapshots.
Function
sumSnapshots(events: String[]) → intExamples
Example 1
events = ["THRESHOLD","TIMER","THRESHOLD","TIMER","THRESHOLD","TIMER","THRESHOLD","TIMER","THRESHOLD","TIMER","TIMEOUT"]return = 20The tenth increment records the threshold snapshot as 10. The following timeout records 10, so the sum is 20.
Example 2
events = ["TIMER","TIMEOUT","THRESHOLD","THRESHOLD","THRESHOLD","THRESHOLD","THRESHOLD","THRESHOLD","THRESHOLD","THRESHOLD","THRESHOLD"]return = 11The timer snapshot is 1. The timer then exits, and nine threshold increments raise the counter to 10, producing a total of 11.
Example 3
events = ["THRESHOLD","THRESHOLD","THRESHOLD","THRESHOLD","THRESHOLD","TIMER","TIMER","TIMER","TIMER","TIMER","TIMER","TIMER","TIMEOUT"]return = 22The fifth timer increment is the tenth total increment, so the threshold snapshot is 10. Two more timer increments occur before timeout records 12.
Constraints
1 <= events.length <= 200000.- Every event is
"THRESHOLD","TIMER", or"TIMEOUT". - The sequence contains exactly one
"TIMEOUT". - The sequence guarantees that both snapshots are recorded before it ends.
More Tesla interview questions
- Alternate Positive and Negative ValuesONSITE INTERVIEW · Seen Aug 2026
- Enumerate Character Paths to a MarkerONSITE INTERVIEW · Seen Aug 2026
- Flatten a Branched List into a Doubly Linked ListPHONE SCREEN · Seen Sep 2026
- Reduce-Based Map and Filter PipelinePHONE SCREEN · Seen Sep 2026
- Resilient Fake API ClientPHONE SCREEN · Seen Sep 2026
- Merge JSON Array FieldsPHONE SCREEN · Seen Aug 2026
- Count Zero-Sum FragmentsOA · Seen Jun 2026
- Maximum Insertions Without Three Consecutive AOA · Seen Jun 2026
- KV Store with RollbackPHONE SCREEN · Seen Apr 2026
- Maximum Number of Workers with Equal Left and Right ShoesOA · Seen Mar 2025
Similar questions at other companies
- Circuit Breaker State MachineWise · ONSITE INTERVIEW
- Concurrent Discounted Card PurchasesBrex · PHONE SCREEN
- Schedule Buffered Video PlaybackNuro · PHONE SCREEN
- Simulate a Queued Multi-Rule Rate LimiterSnowflake · PHONE SCREEN
- First Successful Concurrent API Result Before a TimeoutApple · PHONE SCREEN
- First-Available Seat BookingTarget · OA
- Sustained Overspeed AlertsFleetx · ONSITE INTERVIEW
- Spiral MatrixAthenahealth · ONSITE INTERVIEW
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEW
- Random Dasher Set OperationsDoorDash · ONSITE INTERVIEW