FastPrepLive Top-K Spaces by Active Users
Problems/Live Top-K Spaces by Active Users

Live Top-K Spaces by Active Users

xAI logoxAI● HardFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: This is the reported real-time top-k follow-up to Space membership events. The report does not define a query API, ties, or what to do at zero users. So our version returns a snapshot after every event, breaks ties by Space name, and keeps observed Spaces at count zero.

You are given a list of events, records. Process them in order. Each record is [operation, space, user, timestamp], and the operation is one of these:

  • create creates a new Space and adds its creator as an active user.
  • join adds a user who is not active in that Space yet.
  • leave removes a user who is active in that Space.

After every record, take a snapshot of the top Spaces. A Space can be ranked once it has appeared in any record so far. If all its users leave, it can still be ranked with count zero. The snapshot holds up to k of these Spaces with the most active users right now.

Write each entry as space:count. Sort a snapshot by count from high to low. Break ties by Space name in ascending order.

Return the snapshots in event order.

Function

liveTopSpaces(records: String[][], k: int) → String[][]

Examples

Example 1

records = [["create","abc","u1","1"],["join","abc","u2","2"],["create","def","u3","3"],["leave","abc","u1","4"],["leave","abc","u2","5"]]k = 2return = [["abc:1"],["abc:2"],["abc:2","def:1"],["abc:1","def:1"],["def:1","abc:0"]]

abc starts with u1, then u2 joins, so it has two active users. def is created with one. In the fourth event, u1 leaves abc. Both Spaces now have one active user, so abc comes first by name. In the fifth event, u2 also leaves. Now def ranks ahead of the empty abc, which stays with count zero.

Example 2

records = [["create","z","u1","1"],["create","a","u2","2"],["create","m","u3","3"]]k = 1return = [["z:1"],["a:1"],["a:1"]]

Every create gives its Space one active user, so all counts are equal. With k = 1, each snapshot shows only the Space whose name sorts first so far. After the first event that is z. Then a sorts before z. After m is created, a still sorts first.

Constraints

  • 1 <= records.length <= 100000.
  • 1 <= k <= 100.
  • Every record has exactly four strings. Its create, join, or leave is always a valid step for that Space and user.
  • Timestamps never decrease. The ranking depends only on the order of events.
  • Space and user names are non-empty ASCII strings without :.
  • The total number of returned entries is at most 1000000.

More xAI interview questions

  • Total Active Time by SpaceONSITE 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

  • 15-Minute Chat Session TrackerOpenAI · PHONE SCREEN · ONSITE INTERVIEW
  • LRU Cache with TTLAutodesk · ONSITE INTERVIEW
  • Scheduled Payments with Account AliasesCoinbase · ONSITE INTERVIEW
  • Keyed Bounded Executor ScheduleMicrosoft · ONSITE INTERVIEW
  • Tiered Expiring Item StoreHudson River Trading · PHONE SCREEN · ONSITE INTERVIEW
  • Limit Order Book Matching EngineCitadel · PHONE SCREEN · ONSITE INTERVIEW
  • Expiring Key SetAmazon · ONSITE INTERVIEW
  • Design Authentication ManagerOracle · ONSITE INTERVIEW
  • Ad Score Scheduler With DelayGoogle · ONSITE INTERVIEW
  • Thread-Safe Task Storage and Worker DispatchNuro · ONSITE INTERVIEW
See xAI hiring insights
Saved
class Solution {
    public String[][] liveTopSpaces(String[][] records, int k) {
        // Write your code here.
    }
}
Checking account…