Total Active Time by Space
Updated
Problem statement
Process records in nondecreasing timestamp order. Each record is [operation, space, user, timestamp].
createcreates a new Space and makes its creator active at that timestamp.joinmakes an inactive user active in an existing Space.leaveends that user's current active interval in the Space.
The active-time contribution of a completed interval is leaveTimestamp - startTimestamp. Sum every user's completed intervals for each Space.
Return one string space:total per observed Space, ordered by Space name in ascending lexicographic order. Timestamps and totals use 64-bit integer arithmetic.
Function
totalSpaceActiveTime(records: String[][]) → String[]Examples
Example 1
records = [["create","abc","user_1","1234567000"],["join","abc","user_2","1234567100"],["leave","abc","user_2","1234567300"],["create","def","user_2","1234568000"],["leave","def","user_2","1234568500"],["leave","abc","user_1","1234569000"]]return = ["abc:2200","def:500"]In abc, user_1 contributes 2000 and user_2 contributes 200. In def, user_2 contributes 500.
Example 2
records = [["create","alpha","u1","5"],["leave","alpha","u1","5"],["join","alpha","u1","8"],["leave","alpha","u1","13"],["create","beta","u2","10"],["leave","beta","u2","12"]]return = ["alpha:5","beta:2"]A user may rejoin after leaving. The zero-duration first interval in alpha contributes nothing; its second interval contributes 5.
Constraints
1 <= records.length <= 200000.- Every record has exactly four strings.
operationiscreate,join, orleave.0 <= timestamp <= 10^18, and timestamps are nondecreasing.- Space and user names are non-empty ASCII strings without
:. - Every
createorjoinhas exactly one later matchingleave; lifecycle operations are valid.
More xAI interview questions
- Live Top-K Spaces by Active UsersONSITE INTERVIEW · Seen Jan 2026
- Dynamic Batch Decode TraceONSITE INTERVIEW · Seen Dec 2025
- Nested Transaction Key-Value StoreONSITE INTERVIEW · Seen Oct 2025
- Weighted LRU CacheONSITE INTERVIEW · Seen Oct 2025
- LFU CacheONSITE INTERVIEW · Seen Sep 2025
- Cached Lazy Token BucketsONSITE INTERVIEW · Seen Sep 2025
- Least Recently Used CacheONSITE INTERVIEW · Seen Jun 2025
- Resumable List IteratorPHONE SCREEN · Seen Apr 2026
- Earliest Dataset Readiness in a PipelinePHONE SCREEN · Seen Mar 2026
- Largest Plus SignOA · Seen May 2025
Similar questions at other companies
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Camel CardsRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Message Event AggregationOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Banking System with Top ActivityCapital One · ONSITE INTERVIEWCapital One interview questions
- Linked Perception HistoryWaymo · ONSITE INTERVIEWWaymo interview questions
- Users with a Short Sign-In SessionLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Read a File from Distributed ChunksHudson River Trading · ONSITE INTERVIEWHudson River Trading interview questions
- Calendar Event System with Pagination and IntersectionLead Bank · ONSITE INTERVIEWLead Bank interview questions
- CD Rental SystemOkta · ONSITE INTERVIEWOkta interview questions
- Order Manager with Partial Cancellations and Indexed QueriesStripe · ONSITE INTERVIEWStripe interview questions